Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. java /** * The SimpleQueueSender class consists only of a main method, * which sends several messages to a queue. * * Run this program in conjunction with SimpleQueueReceiver. * Specify a queue name on the command line when you run the * program. By default, the program sends one message. Specify * a number after the queue name to send that number of messages. */ package org.apache.activemq.demo;
  2. import javax.jms.Connection;
  3. import javax.jms.ConnectionFactory;
  4. import javax.jms.Destination;
  5. import javax.jms.JMSException;
  6. import javax.jms.MessageProducer;
  7. import javax.jms.Session;
  8. import javax.jms.TextMessage;
  9. import javax.naming.Context;
  10. import javax.naming.InitialContext;
  11. import javax.naming.NamingException;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory; /** * A simple polymorphic JMS producer which can work with Queues or Topics which * uses JNDI to lookup the JMS connection factory and destination. */
  14. public final class SimpleProducer {
  15.  private static final Logger LOG = LoggerFactory.getLogger(SimpleProducer.class);
  16.  private SimpleProducer() {} /** * @param args the destination name to send to and optionally, the number of * messages to send */
  17.  public static void main(String[] args) {
  18.   Context jndiContext;
  19.   ConnectionFactory connectionFactory;
  20.   Connection connection;
  21.   Session session;
  22.   Destination destination;
  23.   MessageProducer producer;
  24.   String destinationName;
  25.   final int numMsgs;
  26.   if ((args.length < 1) || (args.length > 2)) {
  27.    LOG.info("Usage: java SimpleProducer <destination-name> [<number-of-messages>]");
  28.    System.exit(1);
  29.   }
  30.   destinationName = args[0];
  31.   LOG.info("Destination name is " + destinationName);
  32.   if (args.length == 2) {
  33.    numMsgs = (new Integer(args[1])).intValue();
  34.   } else {
  35.    numMsgs = 1;
  36.   } /* * Create a JNDI API InitialContext object */
  37.   try {
  38.    jndiContext = new InitialContext();
  39.   } catch (NamingException e) {
  40.    LOG.info("Could not create JNDI API context: " + e.toString());
  41.    System.exit(1);
  42.   } /* * Look up connection factory and destination. */
  43.   try {
  44.    connectionFactory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory");
  45.    destination = (Destination) jndiContext.lookup(destinationName);
  46.   } catch (NamingException e) {
  47.    LOG.info("JNDI API lookup failed: " + e);
  48.    System.exit(1);
  49.   } /* * Create connection. Create session from connection; false means * session is not transacted. Create sender and text message. Send * messages, varying text slightly. Send end-of-messages message. * Finally, close the connection. */
  50.   try {
  51.    connection = connectionFactory.createConnection();
  52.    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  53.    producer = session.createProducer(destination);
  54.    TextMessage message = session.createTextMessage();
  55.    for (int i = 0; i < numMsgs; i++) {
  56.     message.setText("This is message " + (i + 1));
  57.     LOG.info("Sending message: " + message.getText());
  58.     producer.send(message);
  59.    } /* * Send a non-text control message indicating end of messages. */
  60.    producer.send(session.createMessage());
  61.   } catch (JMSException e) {
  62.    LOG.info("Exception occurred: " + e);
  63.   } finally {
  64.    if (connection != null) {
  65.     try {
  66.      connection.close();
  67.     } catch (JMSException ignored) {}
  68.    }
  69.   }
  70.  }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement