Probably,
you have suffered a lot searching for the best way of invoking a EBJ 3.1 hosted in a JBoss AS 7.1.
This article
wants only to show you the steps to launch a simple “ Hello
World” EJB and the way of develop an elemental EBJ client accessing via JNDI.
The plan is
to create a Stateless EJB Maven-based project. In this occasion I rule out a Bean
with state because this is a very delicate issue in terms of design and
performance. I tend to have as first option beans without state.
Moreover,
the invocation in this example is thought as a Remote one, despite the lack of
performance given that there are serialization operations involved in the
business request process.
JNDI will
be the mechanism to resolve this issue.
Finally, I
consider the JBoss Application server running as a standalone node.
These are
the steps in order to create a basic bean and deploy it a Jboss
instance.
1) Firstly,
create a Remote-accesing bean with just one method
that returns the message the client provides.
2) Desploy it in the JBoss Application server.
3) Create
the client based on JNDI outlining each element is needed.
4) Execute
the client.
1) CREATION OF A SINGLE REMOTE-ACCESED Bean (Stateless one)
Here you
see the code of a simple EJB without state.
The remote
interface must be defined.
package com.greeting.ejb;
import javax.ejb.Remote;
@Remote
public interface HelloWorldGreeting {
String getGreeting(String msg);
}
Here it is
the bean implementing the Remote interface and is defined without state.
package com.greeting.ejb.bean;
import com.greeting.ejb.HelloWorldGreeting;
import javax.ejb.Stateless;
import javax.ejb.EJB;
import com.greeting.ejb.SpanishGreetingTranslator;
@Stateless
public class HelloWorldGreetingBean implements HelloWorldGreeting {
@EJB SpanishGreetingTranslator
translator;
public String getGreeting(String msg)
{
return translator.translate(msg);
}
The project
is a maven-based one, and the pom.xml I have used is the following. It is
interesting because the file shows the dependencies we need to develop the
bean.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.greeting</groupId>
<artifactId>greetingejb3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>greetingejb3</name>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6 </source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jboss-repository</id>
<name>JBoss Repository</name>
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>Repo maiven</id>
<name>Maven2</name>
<url>http://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>jboss</groupId>
<artifactId>jboss-ejb-api</artifactId>
<version>7.1.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.1_spec</artifactId>
<version>1.0.2.Final</version>
</dependency>
</dependencies>
</project>
Two
Annotations has been used: @Remote and @Stateless. Be aware that the bean has
injected other bean managed by the container (@EJB
SpanishGreetingTranslator translator; ). At this point,
please, do not pay attention to this detail.
The following step is to package and deploy
this Bean in the JBoss application server. For doing both these steps:
1) To compile the code and then,
package it as JAR file. It is important that the file description called
ejb-jar.xml is not needed if the we use annotations. I
generate the jar file named as greetingejb3-0.0.1-SNAPSHOT.jar
2) To deploy in JBoss the jar file to have
ready the EJB called HelloWorldGreetingBean.
For doing this, copy the jar file into the jboss
directory: JBOSS_HOME\standalone\deployments
You can
see something like this in the JBoss console.
Great!! Our
Bean has been deployed properly. Time to go for our client.
2) CREATION
OF THE CLIENT
To create
the client several stages must be taken into account:
1)
Dependencies needed to ejecute the cliente property.
In this
case the following list that you can find in the JBOSS:HOME\modules
directory must be include in the CLASSPATH of the client project.
..\jboss-as-7.1.1.Final\modules\org\jboss\ejb-client\main\jboss-ejb-client-1.0.5.Final.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\ejb3\main\jboss-ejb3-ext-api-2.0.0.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\logging\main\jboss-logging-3.1.0.GA.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\xnio\main\xnio-api-3.0.3.GA.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\sasl\main\jboss-sasl-1.0.0.Final.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\marshalling\river\main\jboss-marshalling-river-1.3.11.GA.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\marshalling\main\jboss-marshalling-1.3.11.GA.jar
..\jboss-as-7.1.1.Final\modules\javax\ejb\api\main\jboss-ejb-api_3.1_spec-1.0.1.Final.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\remoting3\main\jboss-remoting-3.2.3.GA.jar
..\jboss-as-7.1.1.Final\modules\org\jboss\xnio\nio\main\xnio-nio-3.0.3.GA.jar
..\jboss-as-7.1.1.Final\modules\javax\transaction\api\main\jboss-transaction-api_1.1_spec-1.0.0.Final.jar
2) A file
named jboss-ejb-client.properties must be in
the CLASSPATH of the project as well, with the following content:
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=ejb
remote.connection.default.password=test
Be aware
that the user and password must be added to the JBOSS as Application user (no
described here)
Once all
elements are added, here you are the client code:
import javax.naming.*;
import java.util.*;
import com.greeting.*;
import com.greeting.ejb.*;
public class GreetingClient1 {
public static void main(String[] args) throws
Exception{
Hashtable
jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES,
"org.jboss.ejb.client.naming");
Context context = new InitialContext(jndiProperties);
String ejbString="ejb:/greetingejb3-0.0.1-SNAPSHOT//HelloWorldGreetingBean!com.greeting.ejb.HelloWorldGreeting";
HelloWorldGreeting
bean= (HelloWorldGreeting) context.lookup(ejbString);
System.out.println (bean.getGreeting("hello"));
}
}
The most
important topic in this point is how we call the EJB via JNDI. The string used
to do so has the following format:
ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>
app-name:
the EAR name. In this case is not necessary given that our artifact is a .jar
file.
module-name:
name of the .jar file without extension.
distinct-name: Given that out deployment has not a distinct name, this filed is not
necessary.
bean-name:
the name of our bean.
<f-q-c-o-t-r-i> the qualified
name of the Remote interface.
Our string
is the following:
ejb:/greetingejb3-0.0.1-SNAPSHOT//HelloWorldGreetingBean!com.greeting.ejb.HelloWorldGreeting";
Finally,
execute the client and we obtain the result required.
No comments:
Post a Comment