/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package helloworldjmsclient; /** * * @author yatsenko */ /* * JBoss, Home of Professional Open Source * Copyright 2013, 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. */ import java.util.Properties; import java.util.logging.Logger; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSConsumer; import javax.jms.JMSContext; 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/topic/test"; private static final String DEFAULT_MESSAGE_COUNT = "1"; private static final String DEFAULT_USERNAME = "5620SAM"; private static final String DEFAULT_PASSWORD = "5620Sam!"; private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory"; private static final String PROVIDER_URL = "remoting://127.0.0.1:4447"; public static void main(String[] args) throws Exception { Context namingContext = null; JMSContext context = null; try { String userName = System.getProperty("username", DEFAULT_USERNAME); String password = System.getProperty("password", DEFAULT_PASSWORD); // Set up the namingContext for the JNDI lookup final Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, org.jboss.naming.remote.client.InitialContextFactory.class.getName()); env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL)); env.put(Context.SECURITY_PRINCIPAL, userName); env.put(Context.SECURITY_CREDENTIALS, password); namingContext = new InitialContext(env); //namingContext = new InitialContext(); // Perform the JNDI lookups String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY); log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\""); ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.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 = (Destination) namingContext.lookup(destinationString); log.info("Found destination \"" + destinationString + "\" in JNDI"); // Create the JMS context context = connectionFactory.createContext(userName, password); 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++) { context.createProducer().send(destination, content); } // Create the JMS consumer JMSConsumer consumer = context.createConsumer(destination); // Then receive the same number of messages that were sent for (int i = 0; i < count; i++) { String text = consumer.receiveBody(String.class, 5000); log.info("Received message with content " + text); } } catch (Exception e) { log.severe(e.getMessage()); throw e; } finally { if (namingContext != null) { namingContext.close(); } // closing the context takes care of consumer too if (context != null) { context.close(); } } } }