/* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package com.ksi.demo.console; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.naming.InitialContext; import javax.naming.NamingException; /** * Posts a MapMessage. * * @author Kandy Software Inc. * @version 1.0 */ public class JMSDemoClient { private static Logger logger; private QueueConnection queueConnection; private QueueSession queueSession; private Queue requestQueue; private QueueSender queueSender; private int number; private String propertyName; private String propertyValue; public JMSDemoClient() { logger = Logger.getLogger("JMSDemoClient"); } public void setUp() throws JMSException, NamingException { // 1. Initialize JNDI context with server info Properties props = new Properties(); props.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); props.put(InitialContext.PROVIDER_URL, "jnp://localhost:1099"); props.put(InitialContext.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); InitialContext iniCtx = new InitialContext(props); // 2. Look for a connection factory object (administered object) Object tmp = iniCtx.lookup("QueueConnectionFactory"); QueueConnectionFactory qcf = (QueueConnectionFactory) tmp; // 3. Look for the queue (destination object) requestQueue = (Queue) iniCtx.lookup("queue/KSIDemoQueue"); // 4. Obtain a connection queueConnection = qcf.createQueueConnection(); // 5. Request a session queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); // 6. Obtain a sender reference (message producer) queueSender = queueSession.createSender(requestQueue); // 7. Tell the connection to start delivering messages queueConnection.start(); } public void tearDown() throws JMSException { queueConnection.stop(); queueSession.close(); queueConnection.close(); queueSender.close(); } /** * Post messages in a loop. */ public void execute() { try { setUp(); MapMessage message = queueSession.createMapMessage(); message.setInt("Number", number); // Assign properties to this message message.setStringProperty(getPropertyName(), getPropertyValue()); queueSender.send(message); System.out.println("Message posted"); } catch (JMSException e) { logger.log(Level.SEVERE, e.getMessage()); } catch (NamingException e) { logger.log(Level.SEVERE, e.getMessage()); } finally { try { tearDown(); } catch (JMSException e) { logger.log(Level.SEVERE, e.getMessage()); } } } /** * @return the propertyName */ public String getPropertyName() { return propertyName; } /** * @param propertyName * the propertyName to set */ public void setPropertyName(String propertyName) { this.propertyName = propertyName; } /** * @return the propertyValue */ public String getPropertyValue() { return propertyValue; } /** * @param propertyValue * the propertyValue to set */ public void setPropertyValue(String propertyValue) { this.propertyValue = propertyValue; } /** * @return the number */ public int getNumber() { return number; } /** * @param number * the number to set */ public void setNumber(int number) { this.number = number; } /** * Main method * * @param args * @throws Exception */ public static void main(String args[]) throws Exception { JMSDemoClient client = new JMSDemoClient(); client.setNumber(100); client.setPropertyName("Type"); client.setPropertyValue("Even"); client.execute(); } }