View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.jms;
18  
19  import java.util.logging.Logger;
20  import java.util.Properties;
21  
22  import javax.jms.Connection;
23  import javax.jms.ConnectionFactory;
24  import javax.jms.Destination;
25  import javax.jms.MessageConsumer;
26  import javax.jms.MessageProducer;
27  import javax.jms.Session;
28  import javax.jms.TextMessage;
29  import javax.naming.Context;
30  import javax.naming.InitialContext;
31  
32  public class HelloWorldJMSClient {
33      private static final Logger log = Logger.getLogger(HelloWorldJMSClient.class.getName());
34  
35      // Set up all the default values
36      private static final String DEFAULT_MESSAGE = "Hello, World!";
37      private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
38      private static final String DEFAULT_DESTINATION = "jms/queue/test";
39      private static final String DEFAULT_MESSAGE_COUNT = "1";
40      private static final String DEFAULT_USERNAME = "quickstartUser";
41      private static final String DEFAULT_PASSWORD = "quickstartPwd1!";
42      private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
43      private static final String PROVIDER_URL = "remote://localhost:4447";
44  
45      public static void main(String[] args) throws Exception {
46  
47          ConnectionFactory connectionFactory = null;
48          Connection connection = null;
49          Session session = null;
50          MessageProducer producer = null;
51          MessageConsumer consumer = null;
52          Destination destination = null;
53          TextMessage message = null;
54          Context context = null;
55  
56          try {
57              // Set up the context for the JNDI lookup
58              final Properties env = new Properties();
59              env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
60              env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
61              env.put(Context.SECURITY_PRINCIPAL, System.getProperty("username", DEFAULT_USERNAME));
62              env.put(Context.SECURITY_CREDENTIALS, System.getProperty("password", DEFAULT_PASSWORD));
63              context = new InitialContext(env);
64  
65              // Perform the JNDI lookups
66              String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
67              log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
68              connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryString);
69              log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI");
70  
71              String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
72              log.info("Attempting to acquire destination \"" + destinationString + "\"");
73              destination = (Destination) context.lookup(destinationString);
74              log.info("Found destination \"" + destinationString + "\" in JNDI");
75  
76              // Create the JMS connection, session, producer, and consumer
77              connection = connectionFactory.createConnection(System.getProperty("username", DEFAULT_USERNAME), System.getProperty("password", DEFAULT_PASSWORD));
78              session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
79              producer = session.createProducer(destination);
80              consumer = session.createConsumer(destination);
81              connection.start();
82  
83              int count = Integer.parseInt(System.getProperty("message.count", DEFAULT_MESSAGE_COUNT));
84              String content = System.getProperty("message.content", DEFAULT_MESSAGE);
85  
86              log.info("Sending " + count + " messages with content: " + content);
87  
88              // Send the specified number of messages
89              for (int i = 0; i < count; i++) {
90                  message = session.createTextMessage(content);
91                  producer.send(message);
92              }
93  
94              // Then receive the same number of messages that were sent
95              for (int i = 0; i < count; i++) {
96                  message = (TextMessage) consumer.receive(5000);
97                  log.info("Received message with content " + message.getText());
98              }
99          } catch (Exception e) {
100             log.severe(e.getMessage());
101             throw e;
102         } finally {
103             if (context != null) {
104                 context.close();
105             }
106 
107             // closing the connection takes care of the session, producer, and consumer
108             if (connection != null) {
109                 connection.close();
110             }
111         }
112     }
113 }
114