diff --git a/deltaspike-helloworld-jms/README.md b/deltaspike-helloworld-jms/README.md new file mode 100644 index 0000000000..f6813cd137 --- /dev/null +++ b/deltaspike-helloworld-jms/README.md @@ -0,0 +1,216 @@ +deltaspike-helloworld-jms: HelloWorld JMS Example Using DeltaSpike Configuration Properties +====================== +Author: Weston Price, Rafael Benevides +Level: Intermediate +Technologies: JMS, CDI, DeltaSpike +Summary: Demonstrates a standalone (Java SE) JMS client using DeltaSpike configuration properties +Target Product: WFK + +What is it? +----------- + +This quickstart demonstrates the use of external JMS clients with JBoss Enterprise Application Platform 6 or JBoss AS 7 using DeltaSpike configuration properties. + +It contains the following: + +1. A message producer that sends messages to a JMS destination deployed to a JBoss Enterprise Application Platform 6 or JBoss AS 7 server. + +2. A message consumer that receives message from a JMS destination deployed to a JBoss Enterprise Application Platform 6 or JBoss AS 7 server. + + +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. + + +Add an Application User +---------------- +This quickstart uses secured management interfaces and requires that you create an application user to access the running application. Instructions to set up the quickstart application user can be found here: [Add an Application User](../README.md#addapplicationuser) + + +Configure the JBoss Enterprise Application Platform 6 server +--------------------------- + +If you are using the JBoss AS 7 Quickstart distribution, the server configuration file already contains the JMS `test` queue and you can skip this step. + +However, if you are using the JBoss Enterprise Application Platform 6 distribution, you need to add the JMS `test` queue and `topic` to the application server configuration file. You can configure JMS using the JBoss CLI or by manually editing the configuration file. + +#### Modify the Server JMS Configuration using the JBoss CLI Tool + +1. Start the JBoss Enterprise Application Platform 6 or JBoss AS 7 Server by typing the following: + + For Linux: JBOSS_HOME_SERVER_1/bin/standalone.sh -c standalone-full.xml + For Windows: JBOSS_HOME_SERVER_1\bin\standalone.bat -c standalone-full.xml +2. To start the JBoss CLI tool, open a new command line, navigate to the JBOSS_HOME directory, and type the following: + + For Linux: bin/jboss-cli.sh --connect + For Windows: bin\jboss-cli.bat --connect +3. At the prompt, type the following: + + [standalone@localhost:9999 /] jms-queue add --queue-address=testQueue --entries=queue/test,java:jboss/exported/jms/queue/test + [standalone@localhost:9999 /] jms-topic add --topic-address=testTopic --entries=topic/test,java:jboss/exported/jms/topic/test + +#### Modify the Server JMS Configuration Manually (Server must be stopped) + +1. Open the file: JBOSS_HOME/standalone/configuration/standalone-full.xml +2. Add the JMS `test` queue as follows: + * Find the messaging subsystem: + + + * Scroll to the end of this section and add the following XML after the `` end tag but before the `` element: + + + + + + + + + + + + * Save the changes and close the file. + + +Start JBoss Enterprise Application Platform 6 or JBoss AS 7 with the Full Profile +--------------- + +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 full profile: + + For Linux: JBOSS_HOME/bin/standalone.sh -c standalone-full.xml + For Windows: JBOSS_HOME\bin\standalone.bat -c standalone-full.xml + + +Build and Deploy the Quickstart +------------------------- + +To run the quickstart from the command line: + +1. Make sure you have started the JBoss server. See the instructions in the previous section. + +2. Open a command line and navigate to the root of the deltaspike-helloworld-jms quickstart directory: + + cd PATH_TO_QUICKSTARTS/deltaspike-helloworld-jms + +3. Type the following command to compile and execute the quickstart: + + For JBoss Enterprise Application Platform 6 (Maven user settings NOT configured): + + mvn clean compile exec:java -s PATH_TO_QUICKSTARTS/example-settings.xml + + For JBoss AS 7 or JBoss Enterprise Application Platform 6 (Maven user settings configured): + + mvn clean compile exec:java + + gt +Investigate the Console Output +------------------------- + +If the maven command is successful, with the default configuration you will see output similar to this: + + Mar 14, 2012 1:38:58 PM org.jboss.as.quickstarts.jms.HelloWorldJMSClient main + INFO: Attempting to acquire connection factory "jms/RemoteConnectionFactory" + Mar 14, 2012 1:38:58 PM org.jboss.as.quickstarts.jms.HelloWorldJMSClient main + INFO: Found connection factory "jms/RemoteConnectionFactory" in JNDI + Mar 14, 2012 1:38:58 PM org.jboss.as.quickstarts.jms.HelloWorldJMSClient main + INFO: Attempting to acquire destination "jms/queue/test" + Mar 14, 2012 1:38:58 PM org.jboss.as.quickstarts.jms.HelloWorldJMSClient main + INFO: Found destination "jms/queue/test" in JNDI + Mar 14, 2012 1:38:58 PM org.jboss.as.quickstarts.jms.HelloWorldJMSClient main + INFO: Sending 1 messages with content: Hello, World! + Mar 14, 2012 1:38:58 PM org.jboss.as.quickstarts.jms.HelloWorldJMSClient main + INFO: Received message with content Hello, World! + +_Note_: After the above INFO message, you may see the following error. You can ignore the error as it is a well known error message and does not indicate the maven command was unsuccessful in any way. + + Mar 14, 2012 1:38:58 PM org.jboss.naming.remote.protocol.v1.RemoteNamingStoreV1$MessageReceiver handleEnd + ERROR: Channel end notification received, closing channel Channel ID cd114175 (outbound) of Remoting connection 00392fe8 to localhost/127.0.0.1:4447 + + +Optional Properties +------------------- + +This quickstart uses DeltaSpike to inject properties configuration. + +The configuration is injected by using the `@ConfigProperty` annotation + + @Inject + @ConfigProperty(name = "username", defaultValue = "quickstartUser") + private String usernameConfig; + +This quickstart provides for a certain amount of customization for the `mvn:exec` plugin using the system properties. + +Example: + + mvn clean compile exec:java -Dusername=benevides -Dpassword=xxxxx -Dmessage.count=30 + +The following properties can be configured: + +* `username` + + This username is used for both the JMS connection and the JNDI look-up. Instructions to set up the quickstart application user can be found here: [Add an Application User](../README.md#addapplicationuser). + + Default: `quickstartUser` + +* `password` + + This password is used for both the JMS connection and the JNDI look-up. Instructions to set up the quickstart application user can be found here: [Add an Application User](../README.md#addapplicationuser) + + Default: `quickstartPassword` + +* `connection.factory` + + The name of the JMS ConnectionFactory you want to use. + + Default: `jms/RemoteConnectionFactory` + +* `destination` + + The name of the JMS Destination you want to use. + + Default: `jms/queue/test` + +* `message.count` + + The number of JMS messages you want to produce and consume. + + Default: `1` + +* `message.content` + + The content of the JMS TextMessage. + + Default: `"Hello, World!"` + +* `java.naming.provider.url` + + This property allows configuration of the JNDI directory used to lookup the JMS destination. This is useful when the client resides on another host. + + Default: `"localhost"` + + + +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/deltaspike-helloworld-jms/deltaspike-helloworld-jms-logging.properties b/deltaspike-helloworld-jms/deltaspike-helloworld-jms-logging.properties new file mode 100644 index 0000000000..97a2b0e126 --- /dev/null +++ b/deltaspike-helloworld-jms/deltaspike-helloworld-jms-logging.properties @@ -0,0 +1,36 @@ +# 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. +# Specify the handlers to create in the root logger +# (all loggers are children of the root logger) +# The following creates two handlers +handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler + +# Set the default logging level for the root logger +.level = ALL + +# Set the default logging level for new ConsoleHandler instances +java.util.logging.ConsoleHandler.level = ALL + +# Set the default logging level for new FileHandler instances +java.util.logging.FileHandler.level = ALL +java.util.logging.FileHandler.pattern = deltaspike-helloworld-jms.log +java.util.logging.FileHandler.limit = 50000 +java.util.logging.FileHandler.count = 1 + +# Set the default formatter for new ConsoleHandler instances +java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter + +# Set the default logging level for the logger named com.mycompany +org.jboss.as.quickstarts.jms = ALL diff --git a/deltaspike-helloworld-jms/pom.xml b/deltaspike-helloworld-jms/pom.xml new file mode 100644 index 0000000000..fa1c7ea0e3 --- /dev/null +++ b/deltaspike-helloworld-jms/pom.xml @@ -0,0 +1,156 @@ + + + + 4.0.0 + + org.jboss.as.quickstarts + jboss-as-deltaspike-helloworld-jms + 7.1.2-SNAPSHOT + jar + JBoss AS Quickstarts: deltaspike-helloworld-jms + deltaspike-helloworld-jms: Helloworld JMS external producer/consumer client with DeltaSpike property configuration + + http://jboss.org/jbossas + + + Apache License, Version 2.0 + repo + http://www.apache.org/licenses/LICENSE-2.0.html + + + + + + + UTF-8 + + + 1.0.3.Final + + + 1.1.9.Final + + + 7.1.1.Final + 7.3.Final + + + 2.3.1 + 2.2 + 1.1.1 + + + 1.6 + 1.6 + + + + + + + org.jboss.bom + jboss-javaee-6.0-with-deltaspike + ${version.org.jboss.bom} + pom + import + + + + + + + + org.jboss.as + jboss-as-jms-client-bom + ${version.org.jboss.as} + pom + + + + org.jboss.weld.se + weld-se-core + ${version.org.jboss.weld} + + + + + org.apache.deltaspike.core + deltaspike-core-api + + + + + org.apache.deltaspike.core + deltaspike-core-impl + + + + + + jboss-as-deltaspike-helloworld-jms + + + org.codehaus.mojo + exec-maven-plugin + ${version.exec.plugin} + + org.jboss.as.quickstarts.jms.Main + + + java.logging.config.file + ./deltaspike-helloworld-jms-logging.properties + + + + + + maven-jar-plugin + ${version.jar.plugin} + + + + + + org.jboss.as.plugins + jboss-as-maven-plugin + ${version.org.jboss.as.plugins.maven.plugin} + + admin + admin + + + + + maven-compiler-plugin + ${version.compiler.plugin} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + diff --git a/deltaspike-helloworld-jms/src/main/java/org/jboss/as/quickstarts/jms/HelloWorldJMSClient.java b/deltaspike-helloworld-jms/src/main/java/org/jboss/as/quickstarts/jms/HelloWorldJMSClient.java new file mode 100644 index 0000000000..e6ee8c4863 --- /dev/null +++ b/deltaspike-helloworld-jms/src/main/java/org/jboss/as/quickstarts/jms/HelloWorldJMSClient.java @@ -0,0 +1,127 @@ +/* + * 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.jms; + +import java.util.logging.Logger; +import java.util.Properties; + +import javax.enterprise.inject.Default; +import javax.inject.Inject; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Destination; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; + +import org.apache.deltaspike.core.api.config.annotation.ConfigProperty; + +@Default +public class HelloWorldJMSClient { + + private static final Logger log = Logger.getLogger(HelloWorldJMSClient.class.getName()); + + @Inject + @ConfigProperty(name = "username", defaultValue = "quickstartUser") + private String usernameConfig; + + @Inject + @ConfigProperty(name = "password", defaultValue = "quickstartPassword") + private String passwordConfig; + + @Inject + @ConfigProperty(name = "connection.factory", defaultValue = "jms/RemoteConnectionFactory") + private String connectionFactoryConfig; + + @Inject + @ConfigProperty(name = "destination", defaultValue = "jms/queue/test") + private String destinationConfig; + + @Inject + @ConfigProperty(name = "message.count", defaultValue = "1") + private Integer messageCount; + + @Inject + @ConfigProperty(name = "message.content", defaultValue = "Hello, World!") + private String messageContent; + + // Set up all the default values + private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory"; + private static final String PROVIDER_URL = "remote://localhost:4447"; + + public void executeClient() throws Exception { + + Connection connection = null; + TextMessage message = null; + Context context = null; + + try { + // Set up the context for the JNDI lookup + final Properties env = new Properties(); + env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY); + env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL)); + env.put(Context.SECURITY_PRINCIPAL, usernameConfig); + env.put(Context.SECURITY_CREDENTIALS, passwordConfig); + context = new InitialContext(env); + + // Perform the JNDI lookups + log.info("Attempting to acquire connection factory \"" + connectionFactoryConfig + "\""); + ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryConfig); + log.info("Found connection factory \"" + connectionFactoryConfig + "\" in JNDI"); + + log.info("Attempting to acquire destination \"" + destinationConfig + "\""); + Destination destination = (Destination) context.lookup(destinationConfig); + log.info("Found destination \"" + destinationConfig + "\" in JNDI"); + + // Create the JMS connection, session, producer, and consumer + connection = connectionFactory.createConnection(usernameConfig, passwordConfig); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer producer = session.createProducer(destination); + MessageConsumer consumer = session.createConsumer(destination); + connection.start(); + + log.info("Sending " + messageCount + " messages with content: " + messageContent); + + // Send the specified number of messages + for (int i = 0; i < messageCount; i++) { + message = session.createTextMessage(messageContent); + producer.send(message); + } + + // Then receive the same number of messages that were sent + for (int i = 0; i < messageCount; i++) { + message = (TextMessage) consumer.receive(5000); + log.info("Received message with content " + message.getText()); + } + } catch (Exception e) { + log.severe(e.getMessage()); + throw e; + } finally { + if (context != null) { + context.close(); + } + + // closing the connection takes care of the session, producer, and consumer + if (connection != null) { + connection.close(); + } + } + } +} diff --git a/deltaspike-helloworld-jms/src/main/java/org/jboss/as/quickstarts/jms/Main.java b/deltaspike-helloworld-jms/src/main/java/org/jboss/as/quickstarts/jms/Main.java new file mode 100644 index 0000000000..73b2a1cf2d --- /dev/null +++ b/deltaspike-helloworld-jms/src/main/java/org/jboss/as/quickstarts/jms/Main.java @@ -0,0 +1,46 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2012, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.jboss.as.quickstarts.jms; + +import org.jboss.weld.environment.se.Weld; +import org.jboss.weld.environment.se.WeldContainer; + +/** + * @author Rafael Benevides + * + */ +public class Main { + + /** + * @param args + * @throws Exception + */ + public static void main(String[] args) throws Exception { + WeldContainer weld = new Weld().initialize(); + + HelloWorldJMSClient helloWorldJMSClient = weld.instance().select(HelloWorldJMSClient.class).get(); + + helloWorldJMSClient.executeClient(); + } + +} diff --git a/deltaspike-helloworld-jms/src/main/resources/META-INF/beans.xml b/deltaspike-helloworld-jms/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000000..c659db4d31 --- /dev/null +++ b/deltaspike-helloworld-jms/src/main/resources/META-INF/beans.xml @@ -0,0 +1,3 @@ + +