HOW TO CREATE A EJB 3.X PROJECT USING MAVEN IN ECLIPSE – PART 1
HOW TO CREATE A EJB 3.X PROJECT USING MAVEN IN ECLIPSE – PART 2
http://theopentutorials.com/examples/java-ee/ejb3/how-to-create-a-ejb-3-x-project-using-maven-in-eclipse-part-2Exception in thread "main" java.lang.IllegalStateException: No EJB receiver available for handling [appName:,modulename:20161203ejbmavendemos,distinctname:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@6cc4c815
at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:584)
at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:119)
at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:136)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:121)
at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:104)
at com.sun.proxy.$Proxy0.sayHello(Unknown Source)
at com.chc.ejb3.Client.main(Client.java:9)
正确测试:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building EJB3 Client Maven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:exec (default-cli) @ 20161203ejbclientmavendemo ---
afadsfdsa11111111111111111111:ejb:/20161203ejbmavendemo//HelloWorldBean3!com.chc.ejb3.HelloWorld
十二月 03, 2016 3:50:46 下午 org.jboss.ejb.client.EJBClient <clinit>
INFO: JBoss EJB Client version 1.0.5.Final
十二月 03, 2016 3:50:46 下午 org.xnio.Xnio <clinit>
INFO: XNIO Version 3.0.3.GA
十二月 03, 2016 3:50:46 下午 org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.0.3.GA
十二月 03, 2016 3:50:46 下午 org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 3.2.3.GA
十二月 03, 2016 3:50:47 下午 org.jboss.ejb.client.remoting.VersionReceiver handleMessage
INFO: Received server version 1 and marshalling strategies [river]
十二月 03, 2016 3:50:47 下午 org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
INFO: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@6df97b55, receiver=Remoting connection EJB receiver [connection=Remoting connection <12ef181c>,channel=jboss.ejb,nodename=小锦囊-pc]} on channel Channel ID c992125b (outbound) of Remoting connection 33080707 to localhost/127.0.0.1:4447
十二月 03, 2016 3:50:47 下午 org.jboss.ejb.client.remoting.ChannelAssociation$ResponseReceiver handleMessage
WARN: Unsupported message received with header 0xffffffff
Hello,chc welcome to EJB.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.921 s
[INFO] Finished at: 2016-12-03T15:50:47+08:00
[INFO] Final Memory: 8M/178M
[INFO] ------------------------------------------------------------------------
HOW TO CREATE A EJB 3.X PROJECT USING MAVEN IN ECLIPSE – PART 1
In this EJB3 Maven tutorial, we will see how to build and deploy EJB3 in JBoss AS 7 using m2eclipse plugin which provides Maven integration in Eclipse IDE. This tutorial is split into 2 parts with this part containing the steps to develop and deploy the EJB in JBoss AS using Maven whereas the next part shows how to create and run the EJB3 remote client using Maven.
Contents
Environment Used
- Java SE 6
- Eclipse Juno for Java EE Developers (4.2) IDE
- Maven Eclipse Plugin – m2eclipse version 1.2
- EJB 3.x (stateless session bean)
- JBoss Application Server (AS) 7.1.0.Final
Setting up development environment:
Read this page for installing and setting up the environment for creating Maven projects in Eclipse using m2eclipse plugin.
Project Description
- We are going to create a simple EJB 3 HelloWorld stateless session bean project and a remote Java application client which will call/invoke the bean.
- This “HelloWorld” example explains how to develop and deploy EJB3 in JBoss application server 7.
- For testing this “HelloWorld” bean we write a remote Java Application Client (main() method).
Creating New Maven Project in Eclipse
- Open Eclipse IDE and create a new Maven project which can be done in three ways,
- Right click on Project Explorer -> New -> Other… -> Maven -> Maven Project
- File menu -> New -> Other… -> Maven -> Maven Project
- Click on the down arrow on New icon on toolbar -> Other… -> Maven -> Maven Project
- Select Maven Project and click Next.
- This screen prompts for project workspace location. Make sure “Use default Workspace location” and “Create a simple project (skip archetype selection)” are selected and click Next.
We skip the archetype selection because you can copy paste the pom.xml provided below. Otherwise you can select a “maven quickstart” or any “java ee” archetypes.
- Now we have to configure the project by enter the following details and clickFinish.
- Enter Group Id: as “com.theopentutorials.ejb3“
- Enter Artifact Id as “ejbmavendemo“
- Enter Version: as “1.0-SNAPSHOT“
- Packaging: jar
- Name: EJB3 Maven Demo
- Right-click the project, select Properties. Click on “Project Facets” and click “Convert to faceted form”.
- Check the “EJB Module” and select the version as 3.0 or 3.1
- Put Eclipse IDE in Java EE perspective. (Window menu -> Open Perspective)
Creating Session Bean and Bean Interface
- Right click on src/main/java folder -> New -> Session Bean (EJB 3.x)
- Java package name as com.theopentutorials.ejb3
- Enter the Class name as HelloWorldBean
- Select the State type as Stateless
- Check the Remote Business Interface and enter the name ascom.theopentutorials.ejb3.HelloWorld.
- Click Finish
Bean Interface
Copy the following code in the bean interface and save it.
package com.theopentutorials.ejb3;
import javax.ejb.Remote;
@Remote
public interface HelloWorld {
public String sayHello();
}
Bean Class
Copy the following code in the bean class and save it.
package com.theopentutorials.ejb3;
import javax.ejb.Stateless;
@Stateless
public class HelloWorldBean implements HelloWorld {
public HelloWorldBean() {
}
public String sayHello() {
return "Hello World !!!";
}
}
Configuring Maven pom.xml for EJB3
Complete pom.xml
Complete pom.xml is shown below. Open your pom.xml and copy-paste the following content.
<?xml version="1.0"?> <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.theopentutorials.ejb3</groupId> <artifactId>ejbmavendemo</artifactId> <version>1.0-SNAPSHOT</version> <name>EJB3 Maven Demo</name> <packaging>ejb</packaging> <properties> <!-- Explicitly declaring the source encoding eliminates the following message: --> <!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- JBoss dependency versions --> <version.org.jboss.as.plugins.maven.plugin>7.3.Final</version.org.jboss.as.plugins.maven.plugin> <version.org.jboss.spec.jboss.javaee.6.0>3.0.0.Final</version.org.jboss.spec.jboss.javaee.6.0> <!-- other plugin versions --> <version.compiler.plugin>2.3.1</version.compiler.plugin> <version.ejb.plugin>2.3</version.ejb.plugin> <!-- maven-compiler-plugin --> <maven.compiler.target>1.6</maven.compiler.target> <maven.compiler.source>1.6</maven.compiler.source> <!-- Optional: to use jboss-as:run goal --> <!--<jboss-as.home>C:\Users\iByteCode\Desktop\jboss-as-7.1.0.Final</jboss-as.home> --> </properties> <dependencyManagement> <dependencies> <!-- Define the version of JBoss' Java EE 6 APIs we want to use --> <!-- JBoss distributes a complete set of Java EE 6 APIs including a Bill of Materials (BOM). A BOM specifies the versions of a "stack" (or a collection) of artifacts. We use this here so that we always get the correct versions of artifacts. Here we use the jboss-javaee-6.0 stack (you can read this as the JBoss stack of the Java EE 6 APIs). You can actually use this stack with any version of JBoss AS that implements Java EE 6, not just JBoss AS 7! --> <dependency> <groupId>org.jboss.spec</groupId> <artifactId>jboss-javaee-6.0</artifactId> <version>${version.org.jboss.spec.jboss.javaee.6.0}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Import the Common Annotations API (JSR-250), we use provided scope as the API is included in JBoss AS 7 --> <dependency> <groupId>org.jboss.spec.javax.annotation</groupId> <artifactId>jboss-annotations-api_1.1_spec</artifactId> <scope>provided</scope> </dependency> <!-- Import the EJB 3.1 API, we use provided scope as the API is included in JBoss AS 7 --> <dependency> <groupId>org.jboss.spec.javax.ejb</groupId> <artifactId>jboss-ejb-api_3.1_spec</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <!-- Set the name of the deployment --> <plugins> <!-- JBoss AS plugin to deploy the application --> <plugin> <groupId>org.jboss.as.plugins</groupId> <artifactId>jboss-as-maven-plugin</artifactId> <version>${version.org.jboss.as.plugins.maven.plugin}</version> <configuration> <filename>${project.build.finalName}.jar</filename> </configuration> </plugin> <!-- Compiler plugin enforces Java 1.6 compatibility and activates annotation processors --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${version.compiler.plugin}</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-ejb-plugin</artifactId> <version>${version.ejb.plugin}</version> <configuration> <ejbVersion>3.1</ejbVersion> <!-- this is false by default --> <generateClient>true</generateClient> </configuration> </plugin> </plugins> </build> </project>
Maven Plugins used
1. maven-compiler-plugin
The maven-compiler-plugin is used to compile the sources of your project. In the above pom.xml we have used the version 2.3.1 of the plugin with the source and target JDK set to 1.6 under configuration.
We have defined these settings as properties inside <properties> tag and referring it through ${property}.
<version.compiler.plugin>2.3.1</version.compiler.plugin> <!-- maven-compiler-plugin --> <maven.compiler.target>1.6</maven.compiler.target> <maven.compiler.source>1.6</maven.compiler.source>
2. maven-ejb-plugin
This plugin generates J2EE Enterprise Javabean (EJB) file as well as the associated client jar. We specify the ejb version as 3.1 and request the plugin to generate the client by setting the “generateClient” property to true.
3. jboss-as-maven-plugin
The jboss-as-maven-plugin is used to deploy, redeploy, undeploy or run your application in JBoss AS. Under the configuration we specify the build file name same as the project build filename which is by default of the form “artifactid-version” in our case “ejbmavendemo-1.0-SNAPSHOT”.
Required Maven Dependencies for EJB3
1. jboss-javaee-6.0
Defines the version of JBoss’ Java EE 6 APIs we want to use. JBoss distributes a complete set of Java EE 6 APIs including a Bill of Materials (BOM). A BOM specifies the versions of a “stack” (or a collection) of artifacts. We specify this in <dependencyManagement> tag so that we always get the correct versions of artifacts. The type of this dependency itself a pom which contains the required dependencies.
<dependency> <groupId>org.jboss.spec</groupId> <artifactId>jboss-javaee-6.0</artifactId> <version>${version.org.jboss.spec.jboss.javaee.6.0}</version> <type>pom</type> <scope>import</scope> </dependency>
2. jboss-annotations-api_1.1_spec
<dependency> <groupId>org.jboss.spec.javax.annotation</groupId> <artifactId>jboss-annotations-api_1.1_spec</artifactId> <scope>provided</scope> </dependency>
3. jboss-ejb-api_3.1_spec
<dependency> <groupId>org.jboss.spec.javax.ejb</groupId> <artifactId>jboss-ejb-api_3.1_spec</artifactId> <scope>provided</scope> </dependency>
Whenever the pom.xml is changed, update the project to reflect the changes made. To update, Right click on your project in Package Explorer -> Maven -> Update Project … as shown below.
Install EJB3 in local Maven repository
The next step is to build the EJB and client interfaces JARs and install them in your local Maven repository. To do that we use maven’s clean and install phase.
Right-click your pom.xml and select “Run As -> Maven build…” and type the goal as “clean install” as shown below.
You can check your local maven repository (for example, username/.m2/repository/com/theopentutorials) folder which should contain the ejbmavendemo-1.0-SNAPSHOT.jar and client jar file.
When executing this “clean install” if you face any problem such as
Unable to locate the Javac Compiler in: C:\Program Files\Java\jre6\..\lib\tools.jar. Please ensure you are using JDK 1.4 or above and not a JRE (the com.sun.tools.javac.Main class is required) OR then we need to specify the JRE directory to point to JDK instead of JRE directory. To do that, go to Eclipse menu Windows menu -> Preferences -> Java -> Installed JREs. Select the Installed JRE from right pane and click on Edit. For JRE home: select JDK directory and click Finish and OK. |
---|
Deploy EJB3 JAR in JBoss AS
- Now we have to deploy this ejb application in JBoss which can be done in many ways.
Method 1: using jboss-as:deploy maven goal
The JBoss AS server should be running before we can deploy. “jboss-as:deploy” maven goal deploys the application in JBoss server.
- Start JBoss Application Server either using command line or inside Eclipse Servers.
- Right click your pom.xml -> Run as -> Maven Build …. In the Goals: typejboss-as:deploy as shown below. Click on Apply and Run.
Method 2: using jboss-as:run maven goal
You can also use “jboss-as:run” maven goal which starts the application server and deploys your application. But for this “run” goal, we need to add a property (jboss-as.home) in <properties> tag mentioning your “JBOSS_HOME” directory. This is already mentioned in the complete pom.xml (provided above) under <properties> tag. Just uncomment the below given line in that file and mention your JBOSS_HOME directory. If you did not specify the jboss-as.home property then the plugin will download the entire JBoss AS from their site.
<!-- Optional: to use jboss-as:run goal --> <jboss-as.home>C:\Users\iByteCode\Desktop\jboss-as-7.1.0.Final</jboss-as.home>
- Right click your pom.xml -> Run as -> Maven Build …
- In the Goals: type jboss-as:run. Click on Apply and Run.
You can use Web Console to check whether the EJB3 application JAR has been installed in JBoss. Use the URL http://localhost:9990/ and select Manage Deployments under Deployments. You may need a management realm username/password to login. To do so refer this link.
Project Folder Structure
The complete folder structure of this project is shown below.
HOW TO CREATE A EJB 3.X PROJECT USING MAVEN IN ECLIPSE – PART 2
Contents [hide]
- 1 Create New Maven Project in Eclipse
- 2 Create EJB3 client
- 3 EJB3 client pom.xml
- 4 JBoss EJB Client properties
- 5 Project Structure
- 6 Run the project
1、Create New Maven Project in Eclipse
- Open Eclipse IDE and create a new Maven project which can be done in three ways,
- Right click on Project Explorer -> New -> Other… -> Maven -> Maven Project
- File menu -> New -> Other… -> Maven -> Maven Project
- Click on the down arrow on New icon on toolbar -> Other… -> Maven -> Maven Project
- Select Maven Project and click Next.
- This screen prompts for project workspace location. Make sure “Use default Workspace location” and “Create a simple project (skip archetype selection)” are selected and click Next.
- Now we have to configure the project by entering the following details and clickFinish.
- Enter Group Id: as “com.theopentutorials.ejb3.client“
- Enter Artifact Id as “ejbclientmavendemo“
- Enter Version: as “1.0-SNAPSHOT“
- Packaging: jar
- Name: EJB3 Client Maven
Create EJB3 client
- Create a new Java class “ClientUtility” with package “com.theopentutorials.ejb3” under “src/main/java”
- Copy paste the following code.
010203040506070809101112131415161718192021
package
com.theopentutorials.ejb3;
import
java.util.Properties;
import
javax.naming.Context;
import
javax.naming.InitialContext;
import
javax.naming.NamingException;
public
class
ClientUtility {
private
static
Context initialContext;
private
static
final
String PKG_INTERFACES =
"org.jboss.ejb.client.naming"
;
public
static
Context getInitialContext()
throws
NamingException {
if
(initialContext ==
null
) {
Properties properties =
new
Properties();
properties.put(Context.URL_PKG_PREFIXES, PKG_INTERFACES);
initialContext =
new
InitialContext(properties);
}
return
initialContext;
}
}
- Now create another Java class “Client” in the same package and copy the following code.
01020304050607080910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
package
com.theopentutorials.ejb3;
import
javax.naming.Context;
import
javax.naming.NamingException;
import
com.theopentutorials.ejb3.HelloWorld;
public
class
Client {
public
static
void
main(String[] args) {
HelloWorld bean = doLookup();
System.out.println(bean.sayHello());
// 4. Call business logic
}
private
static
HelloWorld doLookup() {
Context context =
null
;
HelloWorld bean =
null
;
try
{
// 1. Obtaining Context
context = ClientUtility.getInitialContext();
// 2. Generate JNDI Lookup name
String lookupName = getLookupName();
// 3. Lookup and cast
bean = (HelloWorld) context.lookup(lookupName);
}
catch
(NamingException e) {
e.printStackTrace();
}
return
bean;
}
private
static
String getLookupName() {
/*
* The app name is the EAR name of the deployed EJB without .ear suffix.
* Since we haven't deployed the application as a .ear, the app name for
* us will be an empty string
*/
String appName = "";
/*
* The module name is the JAR name of the deployed EJB without the .jar
* suffix.
*/
String moduleName = "ejbmavendemo-1.0-SNAPSHOT";
/*
* AS7 allows each deployment to have an (optional) distinct name. This
* can be an empty string if distinct name is not specified.
*/
String distinctName =
""
;
// The EJB bean implementation class name
String beanName =
"HelloWorldBean"
;
// Fully qualified remote interface name
final
String interfaceName =
"com.theopentutorials.ejb3.HelloWorld"
;
// Create a look up string name
String name =
"ejb:"
+ appName +
"/"
+ moduleName +
"/"
+ distinctName
+
"/"
+ beanName +
"!"
+ interfaceName;
return
name;
}
}
EJB3 client pom.xml
Copy paste the following in your pom.xml file.
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
<?
xml
version
=
"1.0"
?>
<
project
xmlns
=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
<
modelVersion
>4.0.0</
modelVersion
>
<
groupId
>com.theopentutorials.ejb3.client</
groupId
>
<
artifactId
>ejbclientmavendemo</
artifactId
>
<
version
>1.0-SNAPSHOT</
version
>
<
name
>EJB3 Client Maven</
name
>
<
packaging
>jar</
packaging
>
<
properties
>
<!-- Explicitly declaring the source encoding eliminates the following
message: -->
<!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered
resources, i.e. build is platform dependent! -->
<
project.build.sourceEncoding
>UTF-8</
project.build.sourceEncoding
>
<!-- JBoss dependency versions -->
<
version.org.jboss.as
>7.1.1.Final</
version.org.jboss.as
>
<
version.org.jboss.as.plugins.maven.plugin
>7.3.Final</
version.org.jboss.as.plugins.maven.plugin
>
<
version.org.jboss.spec.jboss.javaee.6.0
>3.0.0.Final</
version.org.jboss.spec.jboss.javaee.6.0
>
<!-- other plugin versions -->
<
version.compiler.plugin
>2.3.1</
version.compiler.plugin
>
<
version.exec.plugin
>1.2.1</
version.exec.plugin
>
<!-- maven-compiler-plugin -->
<
maven.compiler.target
>1.6</
maven.compiler.target
>
<
maven.compiler.source
>1.6</
maven.compiler.source
>
</
properties
>
<
dependencyManagement
>
<
dependencies
>
<!-- Define the version of JBoss' Java EE 6 APIs we want to use -->
<!-- JBoss distributes a complete set of Java EE 6 APIs including a Bill
of Materials (BOM). A BOM specifies the versions of a "stack" (or a collection)
of artifacts. We use this here so that we always get the correct versions
of artifacts. Here we use the jboss-javaee-6.0 stack (you can read this as
the JBoss stack of the Java EE 6 APIs). You can actually use this stack with
any version of JBoss AS that implements Java EE 6, not just JBoss AS 7! -->
<
dependency
>
<
groupId
>org.jboss.spec</
groupId
>
<
artifactId
>jboss-javaee-6.0</
artifactId
>
<
version
>${version.org.jboss.spec.jboss.javaee.6.0}</
version
>
<
type
>pom</
type
>
<
scope
>import</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>org.jboss.as</
groupId
>
<
artifactId
>jboss-as-ejb-client-bom</
artifactId
>
<
version
>${version.org.jboss.as}</
version
>
<
type
>pom</
type
>
<
scope
>import</
scope
>
</
dependency
>
</
dependencies
>
</
dependencyManagement
>
<
dependencies
>
<!-- Import the transaction spec API, we use runtime scope because we aren't
using any direct reference to the spec API in our client code -->
<
dependency
>
<
groupId
>org.jboss.spec.javax.transaction</
groupId
>
<
artifactId
>jboss-transaction-api_1.1_spec</
artifactId
>
<
scope
>runtime</
scope
>
</
dependency
>
<!-- Import the EJB 3.1 API, we use runtime scope because we aren't using
any direct reference to EJB spec API in our client code -->
<
dependency
>
<
groupId
>org.jboss.spec.javax.ejb</
groupId
>
<
artifactId
>jboss-ejb-api_3.1_spec</
artifactId
>
<
scope
>runtime</
scope
>
</
dependency
>
<!-- We depend on the EJB remote business interfaces of this application -->
<
dependency
>
<
groupId
>com.theopentutorials.ejb3</
groupId
>
<
artifactId
>ejbmavendemo</
artifactId
>
<
type
>ejb-client</
type
>
<
version
>${project.version}</
version
>
</
dependency
>
<!-- JBoss EJB client API jar. We use runtime scope because the EJB client
API isn't directly used in this example. We just need it in our runtime classpath -->
<
dependency
>
<
groupId
>org.jboss</
groupId
>
<
artifactId
>jboss-ejb-client</
artifactId
>
<
scope
>runtime</
scope
>
</
dependency
>
<!-- client communications with the server use XNIO -->
<
dependency
>
<
groupId
>org.jboss.xnio</
groupId
>
<
artifactId
>xnio-api</
artifactId
>
<
scope
>runtime</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>org.jboss.xnio</
groupId
>
<
artifactId
>xnio-nio</
artifactId
>
<
scope
>runtime</
scope
>
</
dependency
>
<!-- The client needs JBoss remoting to access the server -->
<
dependency
>
<
groupId
>org.jboss.remoting3</
groupId
>
<
artifactId
>jboss-remoting</
artifactId
>
<
scope
>runtime</
scope
>
</
dependency
>
<!-- Remote EJB accesses can be secured -->
<
dependency
>
<
groupId
>org.jboss.sasl</
groupId
>
<
artifactId
>jboss-sasl</
artifactId
>
<
scope
>runtime</
scope
>
</
dependency
>
<!-- data serialization for invoking remote EJBs -->
<
dependency
>
<
groupId
>org.jboss.marshalling</
groupId
>
<
artifactId
>jboss-marshalling-river</
artifactId
>
<
scope
>runtime</
scope
>
</
dependency
>
</
dependencies
>
<
build
>
<
plugins
>
<!-- Enforce Java 1.6 -->
<
plugin
>
<
artifactId
>maven-compiler-plugin</
artifactId
>
<
version
>${version.compiler.plugin}</
version
>
<
configuration
>
<
source
>${maven.compiler.source}</
source
>
<
target
>${maven.compiler.target}</
target
>
</
configuration
>
</
plugin
>
<!-- Add the maven exec plugin to allow us to run a java program via maven -->
<
plugin
>
<
groupId
>org.codehaus.mojo</
groupId
>
<
artifactId
>exec-maven-plugin</
artifactId
>
<
version
>${version.exec.plugin}</
version
>
<
executions
>
<
execution
>
<
goals
>
<
goal
>exec</
goal
>
</
goals
>
</
execution
>
</
executions
>
<
configuration
>
<
executable
>java</
executable
>
<
workingDirectory
>${project.build.directory}/exec-working-directory</
workingDirectory
>
<
arguments
>
<!-- automatically creates the classpath using all project dependencies,
also adding the project build directory -->
<
argument
>-classpath</
argument
>
<
classpath
>
</
classpath
>
<
argument
>com.theopentutorials.ejb3.Client</
argument
>
</
arguments
>
</
configuration
>
</
plugin
>
</
plugins
>
</
build
>
</
project
>
|
Maven Plugins used
1. maven-compiler-plugin
The maven-compiler-plugin is used to compile the sources of your project. We specified jdk 1.6 version for source and target classes.
2. exec-maven-plugin
Since our ejb client is a Java program, to run it we use the exec-maven-plugin which helps to execute system and Java programs. We need to specify the executable (i.e. java), classpath, and the java class (com.theopentutorials.ejb3.Client). The classpath is left empty because the plugin includes the necessary classpath arguments based on the dependencies provided.
Maven Dependencies for EJB3 client
In order to run EJB3 client we need to include the following dependencies,
- We depend on the EJB remote business interfaces of this application to run the client. So we need to specify the ejb client jar dependency. The <type> tag with value “ejb-client” is used to specify this project’s dependency on the EJB client jar.
123456
<
dependency
>
<
groupId
>com.theopentutorials.ejb3</
groupId
>
<
artifactId
>ejbmavendemo</
artifactId
>
<
type
>ejb-client</
type
>
<
version
>${project.version}</
version
>
</
dependency
>
- The dependencies jboss-transaction-api_1.1_spec, jboss-ejb-api_3.1_spec, jboss-ejb-client, xnio-api, xnio-nio, jboss-remoting, jboss-sasl, jboss-marshalling-river havescope as runtime because these are all not required for compilation.
- The dependencies jboss-javaee-6.0 and jboss-as-ejb-client-bom underdependencyManagement have scope as import which is used to include dependency management information from a remote POM into the current project. These remote POMs are provided by JBoss which contains the necessary dependencies for running the client.
JBoss EJB Client properties
- Create a file under “src/main/resources” and name it as “jboss-ejb-client.properties”
- Copy paste the following code.
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
Project Structure
Run the project
- If the project shows any error update the project by right clicking the project, Maven -> Update Project
- Then go to your pom.xml, right-click, select Run As -> Maven build
- Specify the goal as exec:exec (provided by exec-maven-plugin)
Output
If there are no errors, you should see the following output.