7 Replies Latest reply: Jun 14, 2012 7:48 PM by Jignesh Patel RSS

EJB and OSGI with JBoss App-Server 7.1.1.Final

Jerry Sky Newbie

Hi,

I have downloaded the actual JBoss Application Server 7 (jboss-as-7.1.1.Final) and I plan a Software-Architecture to use OSGI-Services from EJB-Stateless-Session-Bean. At this reason I implement a small prototype, a simple EJB-Echo-Bean to call a OSGI-Service.

To inject the BundleContext in the Stateless-EJB I use the @Resource-Annotation

  1. Mr. Diesler describe this solution in https://docs.jboss.org/author/display/JBOSGI/Application+Server+Integration to inject the BundleContext.

 

Here is the my simple EJB

@Stateless

public class EchoService implements EchoServiceLocal, EchoServiceRemote {

    @Resource

    BundleContext context;

    EchoIF service;

:

    @PostConstruct

    public void init() {

      :

    }

   @Override

   public String echo(String str) {

      if(service != null) {

          return "OSGI: " + service.echo(str);

      } else if(context != null) {

          return "context: " + str;

      } else {

          return str;

        }

   }

}

 

But, the App-Container cannot deploy my EAR with the EchoService, because they throws a Exception like this:

:

Caused by: java.lang.IllegalArgumentException: Can not set org.osgi.framework.BundleContext field de.echo.EchoSe

rvice.context to org.jboss.osgi.framework.internal.SystemBundleContext

at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146

) [rt.jar:1.6.0_25]

at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150

) [rt.jar:1.6.0_25]

at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63) [rt.jar:1.6.0_25

]

at java.lang.reflect.Field.set(Field.java:657) [rt.jar:1.6.0_25]

at org.jboss.as.ee.component.ManagedReferenceFieldInjectionInterceptorFactory$ManagedReferenceFieldInjec

 

I found many other people with the same problem.

I've tested a few solutions that are described here:

https://community.jboss.org/thread/172622?start=15&tstart=0

http://web.archiveorange.com/archive/v/wr91MTZQ817J3WkPms5p

But none of these Solutions works.

Have anybody a idea?

Need the App-Container a special Module or a special configuration to inject the Bundle-Context in EJB´s?

https://docs.jboss.org/author/display/AS7/OSGi+Subsystem+Configuration

 

thanks,

  Jerry

  • 1. Re: EJB and OSGI with JBoss App-Server 7.1.1.Final
    Thomas Diesler Master

    Have a look at from our latest jbosgi-1.1.0 distribution.

     

    [tdiesler@tdvaio example]$ mvn -Dtarget.container=jboss710 -Dtest=JavaEEIntegrationTestCase install

     

    The code is here: https://github.com/jbosgi/jbosgi/tree/1.1.0/testsuite/example/src/test/java/org/jboss/test/osgi/example/jbossas

     

    For the EJB3 deployment to see the OSGi APIs you need to have a few dependencies defined in your manifest

     

    final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, EJB3_DEPLOYMENT_NAME);
    archive.addClasses(SimpleStatelessSessionBean.class);
    archive.setManifest(new Asset() {
        @Override
        public InputStream openStream() {
            ManifestBuilder builder = ManifestBuilder.newInstance();
            String osgidep = "org.osgi.core,org.jboss.osgi.framework";
            String apidep = ",deployment." + API_DEPLOYMENT_NAME + ":0.0.0";
            builder.addManifestHeader("Dependencies", osgidep + apidep);
            return builder.openStream();
        }
    });
    
  • 2. Re: EJB and OSGI with JBoss App-Server 7.1.1.Final
    Jerry Sky Newbie

    Hi Thomas,

     

    this little line (Dependencies: org.osgi.core,org.jboss.osgi.framework) in the MANIFEST.ME-File

     

    is the point of success without a Exception!

     

    The Bundle-Context is injected!

     

    Thanks Thomas

  • 3. Re: EJB and OSGI with JBoss App-Server 7.1.1.Final
    Rafael Monteiro e Pereira Newbie

    Hi Thomas,

     

    What are the Maven dependencies for the ManifestBuilder/JavaArchive classes ?

    I tried to add the org.jboss.osgi.spi and didn´t work

    Thank you.

  • 4. Re: EJB and OSGI with JBoss App-Server 7.1.1.Final
    Thomas Diesler Master

    You can start from the examples that we provide.

    See https://community.jboss.org/message/724247#724247

     

    mvn dependency:tree

    should tell you

     

    [tdiesler@tdvaio ~]$ cd git/jbosgi-1.1.0/testsuite/example/
    [tdiesler@tdvaio example]$ mvn dependency:tree
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building JBossOSGi Testsuite Examples 1.1.0
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ jboss-osgi-testsuite-example ---
    [INFO] org.jboss.osgi.testsuite:jboss-osgi-testsuite-example:jar:1.1.0
    [INFO] +- org.apache.aries.blueprint:org.apache.aries.blueprint:jar:0.4:provided
    [INFO] +- org.apache.aries.jmx:org.apache.aries.jmx:jar:0.3:provided
    [INFO] +- org.apache.aries.proxy:org.apache.aries.proxy:jar:0.4:provided
    [INFO] +- org.apache.aries:org.apache.aries.util:jar:0.3:provided
    [INFO] +- org.apache.felix:org.apache.felix.configadmin:jar:1.2.8:provided
    [INFO] +- org.apache.felix:org.apache.felix.eventadmin:jar:1.2.6:provided
    [INFO] |  +- org.osgi:org.osgi.core:jar:4.2.0:provided (version managed from 4.0.0)
    [INFO] |  +- org.osgi:org.osgi.compendium:jar:4.2.0:provided
    [INFO] |  \- concurrent:concurrent:jar:1.3.4:provided
    [INFO] +- org.apache.felix:org.apache.felix.log:jar:1.0.0:provided
    [INFO] +- org.apache.felix:org.apache.felix.scr:jar:1.6.0:provided
    [INFO] +- org.jboss.osgi.http:jbosgi-http-api:jar:1.0.5:provided
    [INFO] |  \- javax.servlet:servlet-api:jar:2.5:provided
    [INFO] +- org.jboss.osgi.jmx:jbosgi-jmx:jar:1.0.11:provided
    [INFO] |  \- org.jboss.osgi.jmx:jbosgi-jmx-api:jar:1.0.11:provided
    [INFO] +- org.jboss.osgi.logging:jboss-osgi-logging:jar:1.0.0:provided
    [INFO] +- org.jboss.osgi.repository:jbosgi-repository-api:jar:1.0.5:provided
    [INFO] |  \- org.jboss.osgi.resolver:jbosgi-resolver-api-v2:jar:2.0.0.Beta2:provided
    [INFO] |     +- org.jboss.osgi.metadata:jbosgi-metadata:jar:2.0.2:provided
    [INFO] |     +- org.apache.felix:org.apache.felix.resolver:jar:0.1.0.Beta1:provided
    [INFO] |     \- org.jboss.logging:jboss-logging:jar:3.1.0.GA:provided
    [INFO] +- org.jboss.osgi.repository:jbosgi-repository:jar:1.0.5:provided
    [INFO] +- org.jboss.osgi.xerces:jbosgi-xerces:jar:2.10.0:provided
    [INFO] |  +- xml-apis:xml-apis:jar:1.4.01:provided
    [INFO] |  +- xml-resolver:xml-resolver:jar:1.2:provided
    [INFO] |  \- xerces:xercesImpl:jar:2.10.0:provided
    [INFO] +- org.jboss.spec.javax.ejb:jboss-ejb-api_3.1_spec:jar:1.0.1.Final:provided
    [INFO] +- org.jboss.spec.javax.servlet:jboss-servlet-api_3.0_spec:jar:1.0.0.Final:provided
    [INFO] +- org.ops4j.pax.web:pax-web-extender-war:jar:1.1.2:provided
    [INFO] |  +- org.ops4j.base:ops4j-base-lang:jar:1.2.3:provided
    [INFO] |  +- org.ops4j.base:ops4j-base-util-xml:jar:1.2.3:provided
    [INFO] |  |  \- org.ops4j.base:ops4j-base-util-collections:jar:1.2.3:provided
    [INFO] |  +- org.ops4j.pax.swissbox:pax-swissbox-core:jar:1.4.0:provided
    [INFO] |  |  \- org.ops4j.pax.swissbox:pax-swissbox-lifecycle:jar:1.4.0:provided
    [INFO] |  +- org.ops4j.pax.swissbox:pax-swissbox-extender:jar:1.4.0:provided
    [INFO] |  +- org.ops4j.pax.swissbox:pax-swissbox-optional-jcl:jar:1.4.0:provided
    [INFO] |  \- org.ops4j.pax.swissbox:pax-swissbox-tracker:jar:1.4.0:provided
    [INFO] +- org.ops4j.pax.web:pax-web-jetty-bundle:jar:1.1.2:provided
    [INFO] +- org.ops4j.pax.web:pax-web-jsp:jar:1.1.2:provided
    [INFO] |  +- org.mortbay.jetty:jsp-2.1-glassfish:jar:2.1.v20091210:provided
    [INFO] |  |  \- org.mortbay.jetty:jsp-api-2.1-glassfish:jar:2.1.v20091210:provided
    [INFO] |  \- org.eclipse.jdt.core.compiler:ecj:jar:3.5.1:provided
    [INFO] +- org.jboss.osgi.testing:jbosgi-testing:jar:1.0.2:test
    [INFO] |  +- org.jboss.osgi.spi:jbosgi-spi:jar:3.0.0:test
    [INFO] |  |  +- args4j:args4j:jar:2.0.12:test
    [INFO] |  |  \- org.jboss.osgi.vfs:jbosgi-vfs:jar:1.0.6:test
    [INFO] |  +- org.jboss.shrinkwrap:shrinkwrap-impl-base:jar:1.0.0-cr-1:test
    [INFO] |  |  +- org.jboss.shrinkwrap:shrinkwrap-api:jar:1.0.0-cr-1:test
    [INFO] |  |  \- org.jboss.shrinkwrap:shrinkwrap-spi:jar:1.0.0-cr-1:test
    [INFO] |  \- junit:junit:jar:4.10:test
    [INFO] |     \- org.hamcrest:hamcrest-core:jar:1.1:test
    [INFO] +- org.jboss.osgi.framework:jbosgi-framework-core:jar:1.1.8.Final:test
    [INFO] |  +- org.jboss.msc:jboss-msc:jar:1.0.1.GA:test
    [INFO] |  +- org.jboss.modules:jboss-modules:jar:1.1.1.GA:test
    [INFO] |  +- org.jboss.osgi.deployment:jbosgi-deployment:jar:1.0.12.Final:test
    [INFO] |  +- org.jboss.osgi.resolver:jbosgi-resolver-felix:jar:1.0.13.Final:test
    [INFO] |  |  +- org.jboss.osgi.resolver:jbosgi-resolver-api:jar:1.0.13.Final:test
    [INFO] |  |  \- org.jboss.osgi.resolver:jbosgi-resolver-spi:jar:1.0.13.Final:test
    [INFO] |  \- org.jboss.osgi.vfs:jbosgi-vfs30:jar:1.0.7.Final:test
    [INFO] |     \- org.jboss:jboss-vfs:jar:3.1.0.Final:test
    [INFO] +- org.osgi:org.osgi.enterprise:jar:4.2.0:provided
    [INFO] +- org.jboss.arquillian.junit:arquillian-junit-container:jar:1.0.0.CR7:test
    [INFO] |  +- org.jboss.arquillian.junit:arquillian-junit-core:jar:1.0.0.CR7:test
    [INFO] |  +- org.jboss.arquillian.test:arquillian-test-api:jar:1.0.0.CR7:test
    [INFO] |  |  \- org.jboss.arquillian.core:arquillian-core-api:jar:1.0.0.CR7:test
    [INFO] |  +- org.jboss.arquillian.test:arquillian-test-spi:jar:1.0.0.CR7:test
    [INFO] |  |  \- org.jboss.arquillian.core:arquillian-core-spi:jar:1.0.0.CR7:test
    [INFO] |  +- org.jboss.arquillian.container:arquillian-container-test-api:jar:1.0.0.CR7:test
    [INFO] |  +- org.jboss.arquillian.container:arquillian-container-test-spi:jar:1.0.0.CR7:test
    [INFO] |  +- org.jboss.arquillian.core:arquillian-core-impl-base:jar:1.0.0.CR7:test
    [INFO] |  +- org.jboss.arquillian.test:arquillian-test-impl-base:jar:1.0.0.CR7:test
    [INFO] |  +- org.jboss.arquillian.container:arquillian-container-impl-base:jar:1.0.0.CR7:test
    [INFO] |  |  +- org.jboss.arquillian.config:arquillian-config-api:jar:1.0.0.CR7:test
    [INFO] |  |  \- org.jboss.arquillian.config:arquillian-config-impl-base:jar:1.0.0.CR7:test
    [INFO] |  |     \- org.jboss.shrinkwrap.descriptors:shrinkwrap-descriptors-spi:jar:1.1.0-beta-1:test
    [INFO] |  \- org.jboss.arquillian.container:arquillian-container-test-impl-base:jar:1.0.0.CR7:test
    [INFO] +- org.jboss.logmanager:jboss-logmanager:jar:1.2.0.GA:test
    [INFO] +- org.slf4j:slf4j-jdk14:jar:1.6.4:test
    [INFO] |  \- org.slf4j:slf4j-api:jar:1.6.4:test
    [INFO] \- org.jboss.arquillian.container:arquillian-container-osgi-embedded:jar:1.0.2.Final:test
    [INFO]    +- org.jboss.arquillian.container:arquillian-container-spi:jar:1.0.0.CR7:test
    [INFO]    |  \- org.jboss.shrinkwrap.descriptors:shrinkwrap-descriptors-api:jar:1.1.0-beta-1:test
    [INFO]    +- org.jboss.arquillian.container:arquillian-container-osgi:jar:1.0.2.Final:test
    [INFO]    +- org.jboss.arquillian.testenricher:arquillian-testenricher-osgi:jar:1.0.2.Final:test
    [INFO]    |  \- javax.inject:javax.inject:jar:1:test
    [INFO]    +- org.jboss.arquillian.protocol:arquillian-protocol-osgi:jar:1.0.2.Final:test
    [INFO]    |  \- org.jboss.arquillian.protocol:arquillian-protocol-jmx:jar:1.0.0.CR7:test
    [INFO]    \- org.jboss.shrinkwrap.resolver:shrinkwrap-resolver-impl-maven:jar:1.0.0-beta-5:test
    [INFO]       +- org.jboss.shrinkwrap.resolver:shrinkwrap-resolver-api:jar:1.0.0-beta-5:test
    [INFO]       +- org.jboss.shrinkwrap.resolver:shrinkwrap-resolver-api-maven:jar:1.0.0-beta-5:test
    [INFO]       +- org.sonatype.aether:aether-api:jar:1.8:test
    [INFO]       +- org.sonatype.aether:aether-util:jar:1.8:test
    [INFO]       +- org.sonatype.aether:aether-connector-wagon:jar:1.8:test
    [INFO]       |  +- org.sonatype.aether:aether-spi:jar:1.8:test
    [INFO]       |  \- org.codehaus.plexus:plexus-container-default:jar:1.5.5:test
    [INFO]       |     +- org.codehaus.plexus:plexus-classworlds:jar:2.2.2:test
    [INFO]       |     +- org.apache.xbean:xbean-reflect:jar:3.4:test
    [INFO]       |     |  +- log4j:log4j:jar:1.2.12:test
    [INFO]       |     |  \- commons-logging:commons-logging-api:jar:1.1:test
    [INFO]       |     \- com.google.collections:google-collections:jar:1.0:test
    [INFO]       +- org.apache.maven:maven-aether-provider:jar:3.0.1:test
    [INFO]       |  +- org.apache.maven:maven-model:jar:3.0.1:test
    [INFO]       |  +- org.apache.maven:maven-model-builder:jar:3.0.1:test
    [INFO]       |  +- org.apache.maven:maven-repository-metadata:jar:3.0.1:test
    [INFO]       |  +- org.sonatype.aether:aether-impl:jar:1.8:test
    [INFO]       |  +- org.codehaus.plexus:plexus-component-annotations:jar:1.5.5:test
    [INFO]       |  \- org.codehaus.plexus:plexus-utils:jar:2.0.4:test
    [INFO]       +- org.apache.maven:maven-settings-builder:jar:3.0.1:test
    [INFO]       |  +- org.codehaus.plexus:plexus-interpolation:jar:1.14:test
    [INFO]       |  +- org.apache.maven:maven-settings:jar:3.0.1:test
    [INFO]       |  \- org.sonatype.plexus:plexus-sec-dispatcher:jar:1.3:test
    [INFO]       |     \- org.sonatype.plexus:plexus-cipher:jar:1.4:test
    [INFO]       +- org.apache.maven.wagon:wagon-provider-api:jar:1.0-beta-7:test
    [INFO]       +- org.apache.maven.wagon:wagon-file:jar:1.0-beta-7:test
    [INFO]       \- org.apache.maven.wagon:wagon-http-lightweight:jar:1.0-beta-7:test
    [INFO]          \- org.apache.maven.wagon:wagon-http-shared:jar:1.0-beta-7:test
    [INFO]             \- commons-logging:commons-logging:jar:1.1.1:test
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 14.481s
    [INFO] Finished at: Tue Apr 17 21:48:37 CEST 2012
    [INFO] Final Memory: 12M/160M
    [INFO] ------------------------------------------------------------------------
    
  • 5. Re: EJB and OSGI with JBoss App-Server 7.1.1.Final
    Rafael Monteiro e Pereira Newbie

    Thomas,

     

    Thanks for the reply

     

    It´s possible to make a servlet access an OSGi service ?

    I try to add the following code to my servlet init method::

     

    final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, EJB3_DEPLOYMENT_NAME);
    archive.addClasses(SimpleStatelessSessionBean.class);
    archive.setManifest(new Asset() {
        @Override
        public InputStream openStream() {
            ManifestBuilder builder = ManifestBuilder.newInstance();
            String osgidep = "org.osgi.core,org.jboss.osgi.framework";
            String apidep = ",deployment." + API_DEPLOYMENT_NAME + ":0.0.0";
            builder.addManifestHeader("Dependencies", osgidep + apidep);
            return builder.openStream();
        }
    });
    

     

    But i kepp getting this exception: 

    Caused by: java.lang.IllegalArgumentException: Can not set org.osgi.framework.BundleContext field de.echo.EchoSe

    rvice.context to org.jboss.osgi.framework.internal.SystemBundleContext

     

    Can you help me with this ?

     

    Thanks

  • 6. Re: EJB and OSGI with JBoss App-Server 7.1.1.Final
    Thomas Diesler Master

    As I said, please start from the working example.

    Also, if you have a webapp issue - create a new thread instead of hijacking this ejb3 thread.

  • 7. Re: EJB and OSGI with JBoss App-Server 7.1.1.Final
    Jignesh Patel Newbie

    I got following error while triyng mvn -Dtarget.container=jboss710 -Dtest=JavaEEIntegrationTestCase install

     

    Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.391 sec <<< FAILURE!

    org.jboss.test.osgi.example.jbossas.JavaEEIntegrationTestCase  Time elapsed: 0 sec  <<< ERROR!

    java.lang.RuntimeException: Could not create a new instance of class org.jboss.arquillian.test.impl.EventTestRunnerAdaptor see cause.

            at org.jboss.arquillian.test.spi.SecurityActions.newInstance(SecurityActions.java:170)

            at org.jboss.arquillian.test.spi.TestRunnerAdaptorBuilder.build(TestRunnerAdaptorBuilder.java:52)

            at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:93)

            at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:234)

            at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:133)

            at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:114)

            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

            at java.lang.reflect.Method.invoke(Method.java:597)

            at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:188)

            at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:166)

            at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86)

            at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:101)

            at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74)

    Caused by: java.lang.reflect.InvocationTargetException

            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

     

     

    I am using Maven 3.0.3.

     

    Moreover doc says there are two bundles and wars are deployed but I don't see those are being generated. When I do mvn install or clean. So am I missing any step here.

    The JavaEEIntegrationTestCase deployes two bundles

    • example-javaee-api
    • example-javaee-service

    and two JavaEE archives

    • example-javaee-ejb3
    • example-javaee-servlet