Skip to content

Commit

Permalink
Add an inter-application communication quickstart
Browse files Browse the repository at this point in the history
* Uses EJBs to communicate
* Aliases EJBs as CDI beans to hide details from
  clients of the beans
* Thanks to Jason Greene for part of description
  • Loading branch information
pmuir authored and sgilda committed Jul 16, 2012
1 parent 78e60e2 commit a5270f0
Show file tree
Hide file tree
Showing 22 changed files with 965 additions and 0 deletions.
88 changes: 88 additions & 0 deletions inter-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
Inter-app: Shows how to communicate between two applications using EJB and CDI
======================================================
Author: Pete Muir

What is it?
-----------

This quickstart shows you how to easily communicate between two modular deployments to JBoss AS 7. Two wars, with a shared API jar, are deployed to the app server. EJB is used to provide inter-application communication, with EJB beans alised to CDI beans, making the inter-application communication transparent to clients of the bean.

CDI only provides intra-applicaion injection (i.e within a top level deployment, ear, war, jar etc). This improves performance of the application server, as to satisfy an injection point all possible candidates have to be scanned / analyzed. If inter-app injection was supported by CDI, performance would scale according to the number of deployments you have (the more deployments in the running system, the slower the deployment). Java EE injection uses unique JNDI names for the wiring, so each injection point is O(1). The approach shown here combines the two approaches such that you limit the name based wiring to one location in your code, and the main consumers of components can use CDI injection to reference these name wired components. For the name approach to work though, you still need to publish instances, and EJB singletons allow you to do that with just one extra annotation.


In all, the project has three modules:

* jboss-as-inter-app-A.war - the first war, whiches exposes an EJB singleton, and a simple UI that allows you to read the value set on the bean in appB
* jboss-as-inter-app-B.war - the second war, whiches exposes an EJB singleton, and a simple UI that allows you to read the value set on the bean in appA

System requirements
-------------------

All you need to build this project is Java 6.0 (Java SDK 1.6) or better, Maven 3.0 or better.

The application this project produces is designed to be run on JBoss Enterprise Application Platform 6 or JBoss AS 7.


Configure Maven
---------------

If you have not yet done so, you must [Configure Maven](../README.md#mavenconfiguration) before testing the quickstarts.

Start JBoss Enterprise Application Platform 6 or JBoss AS 7
-------------------------

1. Open a command line and navigate to the root of the JBoss server directory.
2. The following shows the command line to start the server with the web profile:

For Linux: JBOSS_HOME/bin/standalone.sh
For Windows: JBOSS_HOME\bin\standalone.bat


Build and Deploy the Quickstart
-------------------------

_NOTE: The following build command assumes you have configured your Maven user settings. If you have not, you must include Maven setting arguments on the command line. See [Build and Deploy the Quickstarts](../README.md#buildanddeploy) for complete instructions and additional options._

1. Make sure you have started the JBoss Server as described above.
2. Open a command line and navigate to the root directory of this quickstart.
3. Type this command to build and deploy the archive:

mvn clean package jboss-as:deploy
4. This will deploy `shared/target/jboss-as-inter-app-shared.jar`, `appA/target/jboss-as-inter-app-A.war` and `appB/target/jboss-as-inter-app-B.war` to the running instance of the server.

Access the application (For quickstarts that have a UI component)
---------------------


Access the running application in a browser at the following URLs:

* <http://localhost:8080/jboss-as-inter-app-A>
* <http://localhost:8080/jboss-as-inter-app-B>

You are presented with a form that allows you to set the value on the bean in the other application, as well as display of the value on this application's bean. Enter a new value and press "Update and Send!" to update the value on the other application. Do the same on the other application, and hit the button again on the first application. You should see the values shared between the applications.


Undeploy the Archive
--------------------

1. Make sure you have started the JBoss Server as described above.
2. Open a command line and navigate to the root directory of this quickstart.
3. When you are finished testing, type this command to undeploy the archive:

mvn jboss-as:undeploy


Run the Quickstart in JBoss Developer Studio or Eclipse
-------------------------------------
You can also start the server and deploy the quickstarts from Eclipse using JBoss tools. For more information, see [Use JBoss Developer Studio or Eclipse to Run the Quickstarts](../README.md#useeclipse)

Debug the Application
------------------------------------

If you want to debug the source code or look at the Javadocs of any library in the project, run either of the following commands to pull them into your local repository. The IDE should then detect them.

mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc

------------------------------------

98 changes: 98 additions & 0 deletions inter-app/appA/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0"?>
<!-- JBoss, Home of Professional Open Source Copyright 2012, Red Hat, Inc.
and/or its affiliates, and individual contributors by the @authors tag. See
the copyright.txt in the distribution for a full listing of individual contributors.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License. -->
<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>

<artifactId>jboss-as-inter-app-A</artifactId>
<packaging>war</packaging>
<name>JBoss AS Quickstarts: Inter-application (Application A)</name>
<description>Inter-application: Shows how to communicate between two applications using EJB and CDI</description>

<parent>
<groupId>org.jboss.as.quickstarts</groupId>
<artifactId>jboss-as-inter-app-parent</artifactId>
<version>7.1.1.Final</version>
</parent>

<dependencies>

<!-- Import the CDI API, we use provided scope as the API is included
in JBoss AS 7 -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- Import the EJB 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>

<!-- Import the shared API module, which is used by Maven at compilation
time. See the corresponding Dependencies entry in the MANIFEST.mf which defines
the dependency at runtime. -->
<dependency>
<groupId>org.jboss.as.quickstarts</groupId>
<artifactId>jboss-as-inter-app-shared</artifactId>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<!-- Set the name of the war, used as the context root when the app
is deployed -->
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<!-- Java EE 6 doesn't require web.xml, Maven needs to
catch up! -->
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- Define a dependency on the shared API, which will
be used by JBoss Modules at runtime. See the corresponding dependency declared
in the POM, which defines the dependency at compile time. -->
<archive>
<manifestEntries>
<Dependencies>deployment.jboss-as-inter-app-shared.jar</Dependencies>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!-- JBoss AS plugin to deploy war -->
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.1.1.Final</version>
</plugin>
<!-- Compiler plugin enforces Java 1.6 compatibility and activates
annotation processors -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.as.quickstarts.interapp.appA;

import javax.inject.Inject;
import javax.inject.Named;

import org.jboss.as.quickstarts.interapp.shared.Bar;
import org.jboss.as.quickstarts.interapp.shared.Foo;

/**
* <p>
* JSF Controller class that allows manipulation of Foo and Bar.
* </p>
* <p>
* Note that whilst EJBs are used to provide inter application communication, this is not apparent to consumers of Foo and Bar,
* which use CDI style injection.
* </p>
*
* @author Pete Muir
*
*/
@Named
public class ControllerA {

@Inject
private Foo foo;

@Inject
private Bar bar;

public String getFoo() {
return foo.getName();
}

public void setFoo(String name) {
foo.setName(name);
}

public String getBar() {
return bar.getName();
}

public void setBar(String name) {
bar.setName(name);
}

public void sendAndUpdate() {
// No-op
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.as.quickstarts.interapp.appA;

import javax.ejb.Singleton;

import org.jboss.as.quickstarts.interapp.shared.Foo;

/**
* The Foo bean is registered as an EJB singleton, allowing it to be used in other applications.
*
* The {@link Foo} interface, which defines the contract that {@link FooImpl} exposes is placed in a shared jar.
*
* @author Pete Muir
*
*/
@Singleton
public class FooImpl implements Foo {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.as.quickstarts.interapp.appA;

import javax.ejb.EJB;
import javax.enterprise.inject.Produces;

import org.jboss.as.quickstarts.interapp.shared.Bar;

/**
* The Imports class is used to alias EJBs imported from other applications as local CDI beans, thus allowing consumers to
* ignore the details of inter-application communication.
*
* @author Pete Muir
*
*/
public class Imports {

@SuppressWarnings("unused")
@Produces
@EJB(lookup = "java:global/jboss-as-inter-app-B/BarImpl!org.jboss.as.quickstarts.interapp.shared.Bar")
private Bar bar;

private Imports() {
// Disable instantiation of this class
}

}
17 changes: 17 additions & 0 deletions inter-app/appA/src/main/webapp/WEB-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- JBoss, Home of Professional Open Source Copyright 2012, Red Hat, Inc.
and/or its affiliates, and individual contributors by the @authors tag. See
the copyright.txt in the distribution for a full listing of individual contributors.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License. -->
<!-- Marker file indicating CDI should be enabled -->
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">

</beans>
17 changes: 17 additions & 0 deletions inter-app/appA/src/main/webapp/WEB-INF/faces-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<!-- JBoss, Home of Professional Open Source Copyright 2012, Red Hat, Inc.
and/or its affiliates, and individual contributors by the @authors tag. See
the copyright.txt in the distribution for a full listing of individual contributors.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License. -->
<!-- Marker file indicating JSF should be enabled -->
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">

</faces-config>
Loading

0 comments on commit a5270f0

Please sign in to comment.