Niraj Patel
2013-06-17 07:33:06 UTC
Niraj Patel [https://community.jboss.org/people/nirajpatel] created the document:
"Hello World AS JMS - URGENT!"
To view the document, visit: https://community.jboss.org/docs/DOC-48891
--------------------------------------------------------------
*Hi,*
*I am very new to jBoss and I have a client deadline of tomorrow to show a working example of our product using jBoss.I cannot for the life of me figure out how to run the Hello World AS JMS example provided by jboss on my computer. I want to deploy it and obtain the messages as well. How do I do that? Or must I have 2 seperate computers? Please provide a detailed explanation.*
*THANK YOU!*
--------
/*
* 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 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.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;
public class HelloWorldJMSClient {
   private static final Logger log = Logger.getLogger(HelloWorldJMSClient.class.getName());
   // Set up all the default values
   private static final String DEFAULT_MESSAGE = "Hello, World!";
   private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
   private static final String DEFAULT_DESTINATION = "jms/queue/test";
   private static final String DEFAULT_MESSAGE_COUNT = "1";
   private static final String DEFAULT_USERNAME = "quickstartUser";
   private static final String DEFAULT_PASSWORD = "quickstartPassword";
   private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
   private static final String PROVIDER_URL = "remote://localhost:4447";
   public static void main(String[] args) throws Exception {
       ConnectionFactory connectionFactory = null;
       Connection connection = null;
       Session session = null;
       MessageProducer producer = null;
       MessageConsumer consumer = null;
       Destination destination = 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, System.getProperty("username", DEFAULT_USERNAME));
           env.put(Context.SECURITY_CREDENTIALS, System.getProperty("password", DEFAULT_PASSWORD));
           context = new InitialContext(env);
           // Perform the JNDI lookups
           String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
           log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
           connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryString);
           log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI");
           String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
           log.info("Attempting to acquire destination \"" + destinationString + "\"");
           destination = (Destination) context.lookup(destinationString);
           log.info("Found destination \"" + destinationString + "\" in JNDI");
           // Create the JMS connection, session, producer, and consumer
           connection = connectionFactory.createConnection(System.getProperty("username", DEFAULT_USERNAME), System.getProperty("password", DEFAULT_PASSWORD));
           session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
           producer = session.createProducer(destination);
           consumer = session.createConsumer(destination);
           connection.start();
           int count = Integer.parseInt(System.getProperty("message.count", DEFAULT_MESSAGE_COUNT));
           String content = System.getProperty("message.content", DEFAULT_MESSAGE);
           log.info("Sending " + count + " messages with content: " + content);
           // Send the specified number of messages
           for (int i = 0; i < count; i++) {
               message = session.createTextMessage(content);
               producer.send(message);
           }
           // Then receive the same number of messaes that were sent
           for (int i = 0; i < count; 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();
           }
       }
   }
}
--------------------------------------------------------------
Comment by going to Community
[https://community.jboss.org/docs/DOC-48891]
Create a new document in Beginner's Corner at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2075]
"Hello World AS JMS - URGENT!"
To view the document, visit: https://community.jboss.org/docs/DOC-48891
--------------------------------------------------------------
*Hi,*
*I am very new to jBoss and I have a client deadline of tomorrow to show a working example of our product using jBoss.I cannot for the life of me figure out how to run the Hello World AS JMS example provided by jboss on my computer. I want to deploy it and obtain the messages as well. How do I do that? Or must I have 2 seperate computers? Please provide a detailed explanation.*
*THANK YOU!*
--------
/*
* 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 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.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;
public class HelloWorldJMSClient {
   private static final Logger log = Logger.getLogger(HelloWorldJMSClient.class.getName());
   // Set up all the default values
   private static final String DEFAULT_MESSAGE = "Hello, World!";
   private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
   private static final String DEFAULT_DESTINATION = "jms/queue/test";
   private static final String DEFAULT_MESSAGE_COUNT = "1";
   private static final String DEFAULT_USERNAME = "quickstartUser";
   private static final String DEFAULT_PASSWORD = "quickstartPassword";
   private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
   private static final String PROVIDER_URL = "remote://localhost:4447";
   public static void main(String[] args) throws Exception {
       ConnectionFactory connectionFactory = null;
       Connection connection = null;
       Session session = null;
       MessageProducer producer = null;
       MessageConsumer consumer = null;
       Destination destination = 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, System.getProperty("username", DEFAULT_USERNAME));
           env.put(Context.SECURITY_CREDENTIALS, System.getProperty("password", DEFAULT_PASSWORD));
           context = new InitialContext(env);
           // Perform the JNDI lookups
           String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
           log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
           connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryString);
           log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI");
           String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
           log.info("Attempting to acquire destination \"" + destinationString + "\"");
           destination = (Destination) context.lookup(destinationString);
           log.info("Found destination \"" + destinationString + "\" in JNDI");
           // Create the JMS connection, session, producer, and consumer
           connection = connectionFactory.createConnection(System.getProperty("username", DEFAULT_USERNAME), System.getProperty("password", DEFAULT_PASSWORD));
           session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
           producer = session.createProducer(destination);
           consumer = session.createConsumer(destination);
           connection.start();
           int count = Integer.parseInt(System.getProperty("message.count", DEFAULT_MESSAGE_COUNT));
           String content = System.getProperty("message.content", DEFAULT_MESSAGE);
           log.info("Sending " + count + " messages with content: " + content);
           // Send the specified number of messages
           for (int i = 0; i < count; i++) {
               message = session.createTextMessage(content);
               producer.send(message);
           }
           // Then receive the same number of messaes that were sent
           for (int i = 0; i < count; 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();
           }
       }
   }
}
--------------------------------------------------------------
Comment by going to Community
[https://community.jboss.org/docs/DOC-48891]
Create a new document in Beginner's Corner at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2075]