Showing posts with label JEE. Show all posts
Showing posts with label JEE. Show all posts

Tuesday, 24 May 2011

How to bind session beans to JNDI in JBoss

Introduction
To override the default way session beans bind to JNDI in JBoss you can use @LocalBinding or @RemoteBinding annotations. Please note that these annotations are not standard on the EJB3 specification, they are JBoss specific (they are located in org.jboss.ejb3.annotation). This post is based on JBoss Documentation - Binding your beans in JNDI.

1. Maven configuration
The following dependencies need to be added to your pom to get the LocalBinding and RemoteBinding working:

  
        org.jboss.ejb3  
        jboss-ejb3-ext-api  
        1.1.1  
        provided  
          
            
            org.jboss.javaee  
            jboss-ejb-api  
            
            
            org.jboss.metadata  
            jboss-metadata  
            
          
         

2. Local JNDI Binding
The @LocalBinding annotation allows you to change the JNDI name for an EJB local interface. The following code snippet shows the usage:

package com.example.local;   
import org.jboss.ejb3.annotation.LocalBinding;

@Local
@LocalBinding(jndiBinding = "ExampleService/local")
public interface ExampleService { 
   
}

The previous EJB will bind with to JNDI as ExampleService/local. The server log shows the EJB has been deployed as described:

2011-05-08 08:20:20,306 117281 INFO  [org.jboss.ejb3.session.SessionSpecContainer] (RMI TCP Connection(10)-127.0.0.1:) Starting jboss.j2ee:ear=example.ear,jar=example-ejbs-1.0.0.jar,name=ExampleServiceImpl,service=EJB3
2011-05-08 08:20:20,306 117281 INFO  [org.jboss.ejb3.EJBContainer] (RMI TCP Connection(10)-127.0.0.1:) STARTED EJB: com.example.ejbs.ExampleServiceImpl ejbName: ExampleServiceImpl
2010-05-08 08:20:20,353 117328 INFO  [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] (RMI TCP Connection(10)-127.0.0.1:) Binding the following Entries in Global JNDI:

 ExampleService/local - EJB3.x Default Local Business Interface
 example/ExampleServiceImpl/local-com.example.local.ExampleService - EJB3.x Local Business Interface

3. Remote Interface JNDI Binding
The @RemoteBinding annotation allows you to change the JNDI name for an EJB remote interface. The following code snippet shows the usage:

package com.example.remote;
import org.jboss.ejb3.annotation.RemoteBinding;
  
 
@Remote
@RemoteBinding(jndiBinding="ExampleService/remote")
public interface ExampleService { 
    
}

The server log shows the following JNDI names:

2010-05-08 08:20:21,306 117281 INFO  [org.jboss.ejb3.session.SessionSpecContainer] (RMI TCP Connection(10)-127.0.0.1:) Starting jboss.j2ee:ear=example.ear,jar=example-ejbs-1.0.0.jar,name=ExampleServiceImpl,service=EJB3
2010-05-08 08:20:21,306 117281 INFO  [org.jboss.ejb3.EJBContainer] (RMI TCP Connection(10)-127.0.0.1:) STARTED EJB: com.example.ejbs.ExampleServiceImpl ejbName: ExampleServiceImpl
2010-05-08 08:20:21,353 117328 INFO  [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] (RMI TCP Connection(10)-127.0.0.1:) Binding the following Entries in Global JNDI:

 ExampleService/remote - EJB3.x Default Remote Business Interface
 example/ExampleServiceImpl/remote-com.example.remote.ExampleService - EJB3.x Remote Business Interface

4. More over RemoteBindings
By default JBoss EJB3 uses a socket based invoker layer on port 3878 (i.e. socket://0.0.0.0:3878). It is possible, for example, to use SSL as the transport (i.e. sslsocket://0.0.0.0:3843). You can find plenty of documentation on how to enable SSL transport on your EJBs.

You can also enable different types of communication for your beans:

package com.example.remote;   
import org.jboss.ejb3.annotation.RemoteBindings;
import org.jboss.ejb3.annotation.RemoteBinding;

@Remote
@RemoteBindings(
        {
                @RemoteBinding(jndiBinding="custom/ExampleService/remote"),
                @RemoteBinding(jndiBinding="custom/ExampleServiceImpl/remote", clientBindUrl="socket://0.0.0.0:3333"),
                @RemoteBinding(jndiBinding="ssl/ExampleServiceImpl/remote", clientBindUrl="sslsocket://0.0.0.0:3843")
        }
)
public interface ExampleService {
}

The server.log file shows the following JNDI names:

2010-05-08 08:20:20,306 117281 INFO  [org.jboss.ejb3.session.SessionSpecContainer] (RMI TCP Connection(10)-127.0.0.1:) Starting jboss.j2ee:ear=example.ear,jar=example-ejbs-1.0.0.jar,name=ExampleServiceImpl,service=EJB3
2010-05-08 08:20:20,306 117281 INFO  [org.jboss.ejb3.EJBContainer] (RMI TCP Connection(10)-127.0.0.1:) STARTED EJB: com.example.ejbs.ExampleServiceImpl ejbName: ExampleServiceImpl
2010-05-08 08:20:20,353 117328 INFO  [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] (RMI TCP Connection(10)-127.0.0.1:) Binding the following Entries in Global JNDI:

 custom/ExampleService/remote - EJB3.x Default Remote Business Interface
 custom/ExampleServiceImpl/remote - EJB3.x Default Remote Business Interface
 ssl/ExampleServiceImpl/remote - EJB3.x Default Remote Business Interface
 example/ExampleServiceImpl/remote-com.example.remote.ExampleService - EJB3.x Remote Business Interface