Advertisement
Guest User

Active MQ simple queue sender

a guest
Nov 13th, 2010
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. import java.util.Properties;
  2.  
  3. import javax.jms.ObjectMessage;
  4. import javax.jms.Connection;
  5. import javax.jms.QueueConnection;
  6. import javax.jms.Session;
  7. import javax.jms.QueueSession;
  8. import javax.jms.QueueConnectionFactory;
  9. import javax.jms.Queue;
  10. import javax.jms.QueueSender;
  11.  
  12. import javax.naming.InitialContext;
  13. import javax.naming.Context;
  14.  
  15. import org.apache.activemq.ActiveMQConnectionFactory;
  16.  
  17.  
  18. public class SimpleProducer {
  19.    
  20.     private static Properties jndiProps = new Properties();
  21.     static {
  22.         jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
  23.         jndiProps.put(Context.PROVIDER_URL, "rmi://localhost:1099");
  24.     }
  25.     private static InitialContext jndiCtx = null;
  26.    
  27.     private static QueueConnection connection = null;
  28.    
  29.     private static QueueSession session = null;
  30.    
  31.     public SimpleProducer() {
  32.        
  33.         boolean transacted = false;
  34.         int ackMode = Session.AUTO_ACKNOWLEDGE;
  35.         int numberOfMessages = 50;
  36.        
  37.         try {
  38.             //QueueConnectionFactory factory = (QueueConnectionFactory)findObjectInJNDI("ConnectionFactory");
  39.             ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
  40.             //Queue queue = (Queue)findObjectInJNDI("dynamicQueues/aqueue");
  41.             connection = factory.createQueueConnection();
  42.  
  43.             session = connection.createQueueSession(transacted, ackMode);
  44.             Queue queue = session.createQueue("aqueue");
  45.             QueueSender sender = session.createSender(queue);
  46.        
  47.             connection.start();
  48.        
  49.             for (int num = 0; num < numberOfMessages; num++) {
  50.                 ObjectMessage message = session.createObjectMessage();
  51.                 message.setObject(new String(new byte[500]));
  52.                 sender.send(message);
  53.                 Thread.sleep(10);
  54.             }
  55.        
  56.             connection.close();
  57.         } catch (Exception e) {
  58.             e.printStackTrace();
  59.         }
  60.     }
  61.    
  62.     private Object findObjectInJNDI(String name) throws Exception {
  63.        
  64.         if (null == jndiCtx)
  65.             try {
  66.                 jndiCtx = new InitialContext(jndiProps);
  67.             } catch (Exception e) {
  68.                 System.out.println("Could not get initial context");
  69.                 e.printStackTrace();
  70.                 System.exit(-1);
  71.             }
  72.         return jndiCtx.lookup(name);
  73.     }
  74.    
  75.     public static void main(String[] args) {
  76.        
  77.         new SimpleProducer();
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement