by
3
6
3,628
3
Top 1% !
Popular
Famous
Easy-to-find
Specified
OpenSource
Popularity: 3225th place

Published on:

ClassNameEjbclient
InputName0args
InputType0String[]
LanguageJava
LicenseMIT_X11
MethodNamemain
OutputTypevoid

How to invoke the EJB remote interface - Java client application

The codes shows how to remotely invoke the methods exposed by EJB remote interface.
The client app will invoke the remote methods using a JNDI lookup.
The appName and beanName refer to the remote application and ejb names to be referenced by the client app.
Notice the usage of InitialContext, setting variables INITIAL_CONTEXT_FACTORY and PROVIDER_URL. 
Finally you must create a specific user on application server, setting its credentials with variables SECURITY_PRINCIPAL and SECURITY_CREDENTIALS.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/44755/How-to-invoke-the-EJB-remote-interface--?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
package com.test.ejbclient; import java.util.List; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import com.test.example.interfaces.IPerson; import com.test.example.person.*; public class Ejbclient { public static void main(String[] args) { // TODO Auto-generated method stub IPerson person = doLookup(); Person p = person.getPerson(4); System.out.println(p.toString()); List<Person> pList = person.getPersonList(); for (Person person2 : pList) { System.out.println(person2.toString()); } } private static IPerson doLookup() { IPerson bean = null; try { bean = lookupRemoteStatelessPersonEJB(); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bean; } private static String getLookupName() { //The app name final String appName = "PersonWebAppEJB_Final"; // The EJB name which by default is the simple class name of the bean implementation class final String beanName = "PersonEJB"; // the remote view fully qualified class name final String viewClassName = IPerson.class.getName(); // let's do the lookup String url = appName + "/" +beanName + "!" + viewClassName; System.out.println("URL: " + url); return url; } private static IPerson lookupRemoteStatelessPersonEJB() throws NamingException { Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); env.put(Context.PROVIDER_URL, "remote://10.0.2.15:4447/"); String userName = "user"; String password = "password"; env.put(Context.SECURITY_PRINCIPAL, userName); env.put(Context.SECURITY_CREDENTIALS, password); env.put("jboss.naming.client.ejb.context", true); InitialContext context = new InitialContext(env); // let's do the lookup String url = getLookupName(); return (IPerson) context.lookup(url); } }

Other Steps and Solutions

If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus