diff --git a/inter-app/README.md b/inter-app/README.md new file mode 100644 index 0000000000..907d1d9aa8 --- /dev/null +++ b/inter-app/README.md @@ -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: + +* +* + +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 + +------------------------------------ + diff --git a/inter-app/appA/pom.xml b/inter-app/appA/pom.xml new file mode 100644 index 0000000000..6909e1905e --- /dev/null +++ b/inter-app/appA/pom.xml @@ -0,0 +1,98 @@ + + + + 4.0.0 + + jboss-as-inter-app-A + war + JBoss AS Quickstarts: Inter-application (Application A) + Inter-application: Shows how to communicate between two applications using EJB and CDI + + + org.jboss.as.quickstarts + jboss-as-inter-app-parent + 7.1.1.Final + + + + + + + javax.enterprise + cdi-api + provided + + + + + org.jboss.spec.javax.ejb + jboss-ejb-api_3.1_spec + provided + + + + + org.jboss.as.quickstarts + jboss-as-inter-app-shared + provided + + + + + + + ${project.artifactId} + + + maven-war-plugin + 2.1.1 + + + false + + + + deployment.jboss-as-inter-app-shared.jar + + + + + + + org.jboss.as.plugins + jboss-as-maven-plugin + 7.1.1.Final + + + + maven-compiler-plugin + 2.3.1 + + 1.6 + 1.6 + + + + + + + diff --git a/inter-app/appA/src/main/java/org/jboss/as/quickstarts/interapp/appA/ControllerA.java b/inter-app/appA/src/main/java/org/jboss/as/quickstarts/interapp/appA/ControllerA.java new file mode 100644 index 0000000000..0dd48015db --- /dev/null +++ b/inter-app/appA/src/main/java/org/jboss/as/quickstarts/interapp/appA/ControllerA.java @@ -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; + +/** + *

+ * JSF Controller class that allows manipulation of Foo and Bar. + *

+ *

+ * 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. + *

+ * + * @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 + } + +} diff --git a/inter-app/appA/src/main/java/org/jboss/as/quickstarts/interapp/appA/FooImpl.java b/inter-app/appA/src/main/java/org/jboss/as/quickstarts/interapp/appA/FooImpl.java new file mode 100644 index 0000000000..fa35be05ac --- /dev/null +++ b/inter-app/appA/src/main/java/org/jboss/as/quickstarts/interapp/appA/FooImpl.java @@ -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; + } + +} diff --git a/inter-app/appA/src/main/java/org/jboss/as/quickstarts/interapp/appA/Imports.java b/inter-app/appA/src/main/java/org/jboss/as/quickstarts/interapp/appA/Imports.java new file mode 100644 index 0000000000..ca855cfdbb --- /dev/null +++ b/inter-app/appA/src/main/java/org/jboss/as/quickstarts/interapp/appA/Imports.java @@ -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 + } + +} diff --git a/inter-app/appA/src/main/webapp/WEB-INF/beans.xml b/inter-app/appA/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000000..61ae33abb4 --- /dev/null +++ b/inter-app/appA/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,17 @@ + + + + + diff --git a/inter-app/appA/src/main/webapp/WEB-INF/faces-config.xml b/inter-app/appA/src/main/webapp/WEB-INF/faces-config.xml new file mode 100644 index 0000000000..2095c3a6eb --- /dev/null +++ b/inter-app/appA/src/main/webapp/WEB-INF/faces-config.xml @@ -0,0 +1,17 @@ + + + + + + diff --git a/inter-app/appA/src/main/webapp/index.html b/inter-app/appA/src/main/webapp/index.html new file mode 100644 index 0000000000..264d8b86c2 --- /dev/null +++ b/inter-app/appA/src/main/webapp/index.html @@ -0,0 +1,23 @@ + + + + + + + + diff --git a/inter-app/appA/src/main/webapp/index.xhtml b/inter-app/appA/src/main/webapp/index.xhtml new file mode 100644 index 0000000000..1a2025d4fa --- /dev/null +++ b/inter-app/appA/src/main/webapp/index.xhtml @@ -0,0 +1,58 @@ + + + + + + + + +jboss-as-inter-app-A + + + + + +
+ + + + +
+ + + + + Enter message "bar": + + + + + + + + Message "foo": + +
+ +
+
+ + + diff --git a/inter-app/appB/pom.xml b/inter-app/appB/pom.xml new file mode 100644 index 0000000000..ef3c09749b --- /dev/null +++ b/inter-app/appB/pom.xml @@ -0,0 +1,98 @@ + + + + 4.0.0 + + jboss-as-inter-app-B + war + JBoss AS Quickstarts: Inter-application (Application B) + Inter-application: Shows how to communicate between two applications using EJB and CDI + + + org.jboss.as.quickstarts + jboss-as-inter-app-parent + 7.1.1.Final + + + + + + + javax.enterprise + cdi-api + provided + + + + + org.jboss.spec.javax.ejb + jboss-ejb-api_3.1_spec + provided + + + + + org.jboss.as.quickstarts + jboss-as-inter-app-shared + provided + + + + + + + ${project.artifactId} + + + maven-war-plugin + 2.1.1 + + + false + + + + deployment.jboss-as-inter-app-shared.jar + + + + + + + org.jboss.as.plugins + jboss-as-maven-plugin + 7.1.1.Final + + + + maven-compiler-plugin + 2.3.1 + + 1.6 + 1.6 + + + + + + + diff --git a/inter-app/appB/src/main/java/org/jboss/as/quickstarts/interapp/appB/BarImpl.java b/inter-app/appB/src/main/java/org/jboss/as/quickstarts/interapp/appB/BarImpl.java new file mode 100644 index 0000000000..4457ef5742 --- /dev/null +++ b/inter-app/appB/src/main/java/org/jboss/as/quickstarts/interapp/appB/BarImpl.java @@ -0,0 +1,28 @@ +package org.jboss.as.quickstarts.interapp.appB; + +import javax.ejb.Singleton; + +import org.jboss.as.quickstarts.interapp.shared.Bar; + +/** + * The Bar bean is registered as an EJB singleton, allowing it to be used in other applications. + * + * @author Pete Muir + * + */ +@Singleton +public class BarImpl implements Bar { + + private String name; + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + +} diff --git a/inter-app/appB/src/main/java/org/jboss/as/quickstarts/interapp/appB/ControllerB.java b/inter-app/appB/src/main/java/org/jboss/as/quickstarts/interapp/appB/ControllerB.java new file mode 100644 index 0000000000..bc0148b72d --- /dev/null +++ b/inter-app/appB/src/main/java/org/jboss/as/quickstarts/interapp/appB/ControllerB.java @@ -0,0 +1,49 @@ +package org.jboss.as.quickstarts.interapp.appB; + +import javax.inject.Inject; +import javax.inject.Named; + +import org.jboss.as.quickstarts.interapp.shared.Bar; +import org.jboss.as.quickstarts.interapp.shared.Foo; + +/** + *

+ * JSF Controller class that allows manipulation of Foo and Bar. + *

+ *

+ * 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. + *

+ * + * @author Pete Muir + * + */ +@Named +public class ControllerB { + + @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 + } +} diff --git a/inter-app/appB/src/main/java/org/jboss/as/quickstarts/interapp/appB/Imports.java b/inter-app/appB/src/main/java/org/jboss/as/quickstarts/interapp/appB/Imports.java new file mode 100644 index 0000000000..7bd51759df --- /dev/null +++ b/inter-app/appB/src/main/java/org/jboss/as/quickstarts/interapp/appB/Imports.java @@ -0,0 +1,26 @@ +package org.jboss.as.quickstarts.interapp.appB; + +import javax.ejb.EJB; +import javax.enterprise.inject.Produces; + +import org.jboss.as.quickstarts.interapp.shared.Foo; + +/** + * 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-A/FooImpl!org.jboss.as.quickstarts.interapp.shared.Foo") + private Foo foo; + + private Imports() { + // Disable instantiation of this class + } + +} diff --git a/inter-app/appB/src/main/webapp/WEB-INF/beans.xml b/inter-app/appB/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000000..61ae33abb4 --- /dev/null +++ b/inter-app/appB/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,17 @@ + + + + + diff --git a/inter-app/appB/src/main/webapp/WEB-INF/faces-config.xml b/inter-app/appB/src/main/webapp/WEB-INF/faces-config.xml new file mode 100644 index 0000000000..2095c3a6eb --- /dev/null +++ b/inter-app/appB/src/main/webapp/WEB-INF/faces-config.xml @@ -0,0 +1,17 @@ + + + + + + diff --git a/inter-app/appB/src/main/webapp/index.html b/inter-app/appB/src/main/webapp/index.html new file mode 100644 index 0000000000..264d8b86c2 --- /dev/null +++ b/inter-app/appB/src/main/webapp/index.html @@ -0,0 +1,23 @@ + + + + + + + + diff --git a/inter-app/appB/src/main/webapp/index.xhtml b/inter-app/appB/src/main/webapp/index.xhtml new file mode 100644 index 0000000000..0b23cf142c --- /dev/null +++ b/inter-app/appB/src/main/webapp/index.xhtml @@ -0,0 +1,57 @@ + + + + + + + + +jboss-as-inter-app-B + + + + + +
+ + + + +
+ + + + + Enter message "foo": + + + + + + + + Message "bar": +
+ +
+
+ + + diff --git a/inter-app/pom.xml b/inter-app/pom.xml new file mode 100644 index 0000000000..a1494030fa --- /dev/null +++ b/inter-app/pom.xml @@ -0,0 +1,98 @@ + + + + 4.0.0 + + org.jboss.as.quickstarts + jboss-as-inter-app-parent + 7.1.1.Final + pom + JBoss AS Quickstarts: Inter-application + Inter-application: Shows how to communicate between two applications using EJB and CDI + + http://jboss.org/jbossas + + + Apache License, Version 2.0 + repo + http://www.apache.org/licenses/LICENSE-2.0.html + + + + + + + UTF-8 + + + + + + + + org.jboss.spec + jboss-javaee-6.0 + 3.0.0.Final + pom + import + + + + + org.jboss.as.quickstarts + jboss-as-inter-app-shared + ${project.version} + + + + + + + + shared + appA + appB + + + + + ${project.artifactId} + + + + org.jboss.as.plugins + jboss-as-maven-plugin + 7.1.1.Final + false + + true + + + + + + + + diff --git a/inter-app/shared/pom.xml b/inter-app/shared/pom.xml new file mode 100644 index 0000000000..183d9f12dd --- /dev/null +++ b/inter-app/shared/pom.xml @@ -0,0 +1,62 @@ + + + + 4.0.0 + + jboss-as-inter-app-shared + jar + JBoss AS Quickstarts: Inter-application (Shared API jar) + Inter-application: Shows how to communicate between two applications using EJB and CDI + + + org.jboss.as.quickstarts + jboss-as-inter-app-parent + 7.1.1.Final + + + + + + + org.jboss.spec.javax.ejb + jboss-ejb-api_3.1_spec + provided + + + + + + + + + org.jboss.as.plugins + jboss-as-maven-plugin + 7.1.1.Final + + + + maven-compiler-plugin + 2.3.1 + + 1.6 + 1.6 + + + + + + + diff --git a/inter-app/shared/src/main/java/org/jboss/as/quickstarts/interapp/shared/Bar.java b/inter-app/shared/src/main/java/org/jboss/as/quickstarts/interapp/shared/Bar.java new file mode 100644 index 0000000000..46bf841161 --- /dev/null +++ b/inter-app/shared/src/main/java/org/jboss/as/quickstarts/interapp/shared/Bar.java @@ -0,0 +1,18 @@ +package org.jboss.as.quickstarts.interapp.shared; + +import javax.ejb.Local; + +/** + * Bar is provided in a shared API jar, that can be referenced by any application wishing to. + * + * @author Pete Muir + * + */ +@Local +public interface Bar { + + public void setName(String name); + + public String getName(); + +} diff --git a/inter-app/shared/src/main/java/org/jboss/as/quickstarts/interapp/shared/Foo.java b/inter-app/shared/src/main/java/org/jboss/as/quickstarts/interapp/shared/Foo.java new file mode 100644 index 0000000000..5fddeec771 --- /dev/null +++ b/inter-app/shared/src/main/java/org/jboss/as/quickstarts/interapp/shared/Foo.java @@ -0,0 +1,18 @@ +package org.jboss.as.quickstarts.interapp.shared; + +import javax.ejb.Local; + +/** + * Bar is provided in a shared API jar, that can be referenced by any application wishing to. + * + * @author Pete Muir + * + */ +@Local +public interface Foo { + + public void setName(String name); + + public String getName(); + +} diff --git a/pom.xml b/pom.xml index f92b367967..b20dfa45fd 100644 --- a/pom.xml +++ b/pom.xml @@ -68,6 +68,7 @@ hibernate4 + inter-app kitchensink kitchensink-ear kitchensink-html5-mobile