EJB3 – Creating a test client

Here you will learn on how to create a test client for EJB 3.First, create a simple log4j.properties file in the src folder. Add the following content:

<em>### direct log messages to stdout ### </em><em>log4j.appender.stdout=org.apache.log4j.ConsoleAppender </em><em>log4j.appender.stdout.Target=System.out </em><em>log4j.appender.stdout.layout=org.apache.log4j.PatternLayout </em><em>log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n </em><em>### direct messages to file hibernate.log ### </em><em>#log4j.appender.file=org.apache.log4j.FileAppender </em><em>#log4j.appender.file.File=hibernate.log </em><em>#log4j.appender.file.layout=org.apache.log4j.PatternLayout </em><em>#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n </em><em>### set log levels - for more verbose logging change 'info' to 'debug' ### </em><em>log4j.rootLogger=debug, stdout</em>

Then create a class named FirstEJB3TutorialClient in the package test.come.home.library. You can create a file named jndi.properties in the src directory and use
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
context = new InitialContext();
<em> </em>
Or configure the JNDI in your application:
 <em>Properties properties = new Properties(); </em><em>properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory" </em><em>); </em><em>properties.put("java.naming.factory.url.pkgs","=org.jboss.naming:org.jnp.interfaces"); </em><em>properties.put("java.naming.provider.url","localhost:1099"); </em><em>Context context = new InitialContext(properties);</em>

Below we have the full source code of the test client:
 package come.home.library;
import java.util.Properties;
<em>import javax.naming.Context; </em><em>import javax.naming.InitialContext; </em><em>import javax.naming.NamingException; </em><em>import com.home.library.BookTestBean; </em><em>import com.home.library.BookTestBeanRemote; </em><em>public class FirstEJB3TutorialClient { </em><em>/** </em><em>* @param args </em><em>*/ </em><em>public static void main(String[] args) { </em><em>/* get a initial context. By default the settings in the file </em><em>* jndi.properties are used. </em><em>* You can explicitly set up properties instead of using the file. </em><em>Properties properties = new Properties(); </em><em>properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory" </em><em>); </em><em>properties.put("java.naming.factory.url.pkgs","=org.jboss.naming:org.jnp.interfaces"); </em><em>properties.put("java.naming.provider.url","localhost:1099"); </em><em>*/ </em><em>Context context; </em><em>try </em><em>{ </em><em>context = new InitialContext(); </em><em>BookTestBeanRemote beanRemote = (BookTestBeanRemote) </em><em>context.lookup(BookTestBean.RemoteJNDIName); </em><em>beanRemote.test(); </em><em>} catch (NamingException e) </em><em>{ </em><em>e.printStackTrace(); </em><em>throw new RuntimeException(e); </em><em>} </em><em>} </em><em>}</em>

Now you have created your first EJB 3 application.