5/11/2012

Standalone Java Client for JBoss AS 7.1.1

In this previous post, I created a sample Java standalone client connecting to JBoss AS 5 & 6. Now let's look at how to do it in JBoss AS 7.1.1.

This sample application consists of a stateless EJB, its remote business interface and a standalone java client that looks up the EJB and invokes its business method.

ClientIF.java
package test;

public interface ClientIF {
public String clientHello();
}
ClientBean.java
package test;
import javax.ejb.*;

@Stateless
@Remote
public class ClientBean implements ClientIF {
public String clientHello() {
return "In clientHello of " + this;
}
}
Client.java
package test;

import javax.naming.*;

public class Client {
public static void main(String args[]) throws Exception {
ClientIF clientBean = InitialContext.doLookup(args[0]);
System.out.printf("Result from clientBean.clientHello(): %s%n%n",
clientBean.clientHello());
}
}
In addition, you also need to prepare a jndi.properties file, and a jboss-ejb-client.properties file, both should be in the client classpath:
# jndi.properties
#
java.naming.factory.initial=org.jboss.naming.remote.client.InitialContextFactory
java.naming.factory.url.pkgs=org.jboss.ejb.client.naming
java.naming.provider.url=remote://localhost:4447
java.naming.security.principal=test
java.naming.security.credentials=1234
# jboss-ejb-client.properties
#
endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false

remote.connections=default

remote.connection.default.host=localhost
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

remote.connection.default.username=appuser
remote.connection.default.password=apppassword

With the above 5 source and config files in place, the project structure looks like this:
jboss-ejb/
.........jndi.properties
.........jboss-ejb-client.properties
.........test/
.............ClientIF.java
.............ClientBean.java
.............Client.java
While in project root directory, compile and package the ejb jar:
javac -cp $JBOSS_HOME/bin/client/jboss-client.jar test/*java
jar cvf client-ejb.jar test/ClientIF.class test/ClientBean.class
If JBoss AS is not already running, start it in a new terminal, and add the application user referenced in the above jndi.properties (add-user.sh is an interactive script). When prompted, enter user name test and password 1234.
cd $JBOSS_HOME/bin
./standalone.sh

./add-user.sh
To deploy the ejb jar, simply copy it to JBoss AS deploy directory:
cp client-ejb.jar $JBOSS_HOME/standalone/deployments/
Finally, run our standalone Java client that looks up and invokes the remote EJB:
java -cp $JBOSS_HOME/bin/client/jboss-client.jar:. test.Client ejb:/client-ejb//ClientBean\!test.ClientIF

May 11, 2012 10:21:16 PM org.xnio.Xnio <clinit>
INFO: XNIO Version 3.0.3.GA
May 11, 2012 10:21:16 PM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.0.3.GA
May 11, 2012 10:21:16 PM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 3.2.3.GA
May 11, 2012 10:21:17 PM org.jboss.ejb.client.EJBClient <clinit>
INFO: JBoss EJB Client version 1.0.5.Final
May 11, 2012 10:21:17 PM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
INFO: Received server version 1 and marshalling strategies [river]
May 11, 2012 10:21:17 PM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
INFO: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@53f64158, receiver=Remoting connection EJB receiver [connection=Remoting connection <a995a79>,channel=jboss.ejb,nodename=m-2]} on channel Channel ID bc3ed9ca (outbound) of Remoting connection 2e00e753 to localhost/127.0.0.1:4447
May 11, 2012 10:21:17 PM org.jboss.ejb.client.remoting.ChannelAssociation$ResponseReceiver handleMessage
WARN: Unsupported message received with header 0xffffffff
Result from clientBean.clientHello(): In clientHello of test.ClientBean@4783165b
The client class does a generic lookup, taking lookup jndi name from application arg. In the above java command line, ejb:/client-ejb//ClientBean!test.ClientIF is the jndi name for our remote EJB. This jndi name is constructed from the ejb module name, bean name, bean distinct name, and its remote business interface. More details are in this JBoss docs page.

12 comments:

Anonymous said...

Very helpful and clear explanation, it has saved my weekend. To configure JNDI and EJB context outside of the Java code is a clean and convenient solution. And the main point is, that works!!!

Unknown said...

Hi...Thanks a lot..Its a beautiful tutorial.
It has helped me a lot.
I am facing a hurdle at the last step.
When I run the java command, I am getting NoClassDefFound error :

Exception in thread "main" java.lang.NoClassDefFoundError: test/Client
Caused by: java.lang.ClassNotFoundException: test.Client

What can be the reason?
Help requested..Thanks a lot..!!

javahowto said...

looks like a client classpath problem. My client run command has the current directory (.) at the end of the -cp option. In the compile step, I also have javac to put compiled class files in the same place as their java source file.

Unknown said...

hi..thanks for the response....!!!

I am basically ccopying as mentioned in the tutorial.....

so what can be done now?
what are the options?

Anonymous said...

WARN: Unsupported message received with header 0xffffffff

Do you know where this warning comes from and what it means?

Prithviraj said...
This comment has been removed by the author.
Prithviraj said...

How do you use jndi.properties file? I do not see that you are using it anywhere while getting access to JNDI context.

EduCzar said...

Very Good tutorial.
Please make sure you select "Application user" while adding new user.

Rick Gan said...

Hi, just want to say you have show me a very great example for me to start a standalone program with JBoss AS 7.1.

I am on a research of achieving SSO for Jboss and Active Directory and i am trying to extend your example here to achieve that but have no idea where to start, do you have any idea how or what can I do to achieve it????

Thanks alot and you can reply me with this email: shenglee83@gmail.com

the procrastinator......... said...

Hi people,
I am a newbie to Jboss.
I followed the steps.I have successfully Deployed the JAR.But cannot run using the command.
Please suggest how to run the JAR.

the error is :Error: Could not find or load main class test.Client
input in cmd is :C:\jboss-as-web-7.0.2.Final>java -cp $JBOSS_HOME/bin/client/jboss-client.jar:. t
est.Client ejb:/client-ejb//ClientBean\!test.ClientIF

the procrastinator......... said...

My jboss version is 7.0.2
Thanks in advance

Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions












Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai