/* * JBoss, Home of Professional Open Source. * Copyright 2009, Red Hat Middleware LLC, 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. */ import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; /** * * @author rsvoboda@redhat.com */ public class ProducerApp { /** * @param args the command line arguments */ public static void main(String[] args) { Context context = null; Connection connection = null; try { Properties properties1 = new Properties(); properties1.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); properties1.setProperty("java.naming.provider.url", "jnp://" + args[0] + ":1099"); properties1.setProperty("java.naming.factory.url.pkgs", "org.jnp.interfaces.NamingContextFactory"); context = new InitialContext(properties1); Destination dest = (Destination) context.lookup("/queue/InQueue"); ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("/ConnectionFactory"); connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); MessageProducer producer = session.createProducer(dest); TextMessage message = null; for (int i = 0; i < 50000; i++) { try { message = session.createTextMessage(i + "HornetQ test message."); message.setIntProperty("count", i); producer.send(message); System.out.println("Sent message : " + i); } catch (Exception ex) { System.out.println("Sending exception ... " + ex); Thread.sleep(2000); try { producer.send(message); System.out.println("RETRY Sent message: " + i); } catch (Exception exc) { System.out.println("Sending exception for retry ... " + exc); exc.printStackTrace(); } } } producer.close(); session.close(); System.out.println("Done ...."); } catch (Exception e) { Logger.getLogger(ProducerApp.class.getName()).log(Level.SEVERE, null, e); } finally { if (connection != null) { try { connection.close(); } catch (JMSException ex) { Logger.getLogger(ProducerApp.class.getName()).log(Level.SEVERE, null, ex); } } if (context != null) { try { context.close(); } catch (NamingException ex) { Logger.getLogger(ProducerApp.class.getName()).log(Level.SEVERE, null, ex); } } } } }