Continuing from part 1 and 2, this part will cover the following porting steps in

1.4 Integrate With EJB 3.0

  • 1.4.1 Define a Remote Interface
  • 1.4.2 Implement a Stateless Session Bean
  • 1.4.3 Build Jar
  • 1.4.4 Run an EJB Client


1.4 Inegrate with EJB 3.0

To integrate with EJb 3.0 (Stateless session bean) use generated stubs as helper classes.

1.4.1 Define a Remote Interface

So first thing is to define a Remote Interface.


package au.com.hello.integrate;

public interface EJB3Remote
{
String echo(String input);
}



1.4.2 Implement a Stateless Session Bean

Now implement that interface on a stateless bean


package au.com.hello.integrate;

import java.util.ArrayList;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.InitialContext;
import javax.xml.ws.BindingProvider;

import org.jboss.annotation.ejb.RemoteBinding;
import org.jboss.logging.Logger;


// standard EJB3 annotations
@Remote(EJB3Remote.class)
@RemoteBinding(jndiBinding = "/ejb3/EJB3Client")
@Stateless

public class EJB3Client implements EJB3Remote
{
// Provide logging
private static Logger log = Logger.getLogger(EJB3Client.class);
public String echo(String inStr)
{
log.info("echo: " + inStr);


au.com.hello.integrate.GetMemberTestService service = new au.com.hello.integrate.GetMemberTestService();

//web services generated stub interface

au.com.hello.integrate.GetMemberTestPortType port =
service.getPort(au.com.hello.integrate.GetMemberTestPortType.class) ;

BindingProvider bp = (BindingProvider) port;
bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "test");
bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "test");

log.info ("Got My logger going -2");

// This the call to web services generated stub interface ~~

String response = port.getMemberTest(inStr);

return response;
}
}



1.4.3 Build Jar
Now build the jar file containing generated stubs and EJB remote interface and Ejb implementation
Structure for jar file is.

vsm@jm000606:deploy$ jar -tvf ejbservref_client.jar
0 Wed Sep 12 02:55:26 AUSEST 20 META-INF/
106 Wed Sep 12 02:55:24 AUSEST 20 META-INF/MANIFEST.MF
0 Wed Sep 12 02:55:26 AUSEST 20 au/
0 Wed Sep 12 02:55:26 AUSEST 20 au/com/
0 Wed Sep 12 02:55:26 AUSEST 20 au/com/hello/
0 Wed Sep 12 02:55:26 AUSEST 20 au/com/hello/integrate/
1950 Wed Sep 12 02:55:26 AUSEST 20 au/com/hello/integrate/EJB3Client.class
180 Wed Sep 12 02:55:26 AUSEST 20 au/com/hello/integrate/EJB3Remote.class
887 Wed Sep 12 02:55:26 AUSEST 20 au/com/hello/integrate/GetMemberTest.class
849 Wed Sep 12 02:55:26 AUSEST 20 au/com/hello/integrate/GetMemberTestPortType.class
931 Wed Sep 12 02:55:26 AUSEST 20 au/com/hello/integrate/GetMemberTestResponse.class
1640 Wed Sep 12 02:55:26 AUSEST 20 au/com/hello/integrate/GetMemberTestService.class
2073 Wed Sep 12 02:55:26 AUSEST 20 au/com/hello/integrate/ObjectFactory.class
279 Wed Sep 12 02:55:26 AUSEST 20 au/com/hello/integrate/package-info.class


Deploy this jar file and make sure it is deployed successfully.

1.4.4 Run an EJB Client


package au.com.hello.integrate.client;

import java.util.Hashtable;

import javax.naming.InitialContext;
import au.com.hello.integrate.EJB3Remote;

public class TestEjbService {

public static void main(String[] args) throws Exception
{

InitialContext ctx = new InitialContext();

EJB3Remote client = (EJB3Remote) ctx.lookup("ejb3/EJB3Client");

System.out.println("1 + 1 = " + client.echo("HI MATHEW"));
}


Other useful tips on J2EE
Del.icio.us Digg! My StumbleUpon Page