Guest User

Untitled

a guest
Apr 29th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. package activemq.test;
  2.  
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. import javax.jms.Connection;
  8. import javax.jms.Destination;
  9.  
  10. import org.apache.activemq.api.core.ActiveMQException;
  11. import org.apache.activemq.api.core.SimpleString;
  12. import org.apache.activemq.api.core.TransportConfiguration;
  13. import org.apache.activemq.api.core.client.ActiveMQClient;
  14. import org.apache.activemq.api.core.client.ClientConsumer;
  15. import org.apache.activemq.api.core.client.ClientMessage;
  16. import org.apache.activemq.api.core.client.ClientProducer;
  17. import org.apache.activemq.api.core.client.ClientSession;
  18. import org.apache.activemq.api.core.client.ClientSessionFactory;
  19. import org.apache.activemq.api.core.client.MessageHandler;
  20. import org.apache.activemq.api.core.client.ServerLocator;
  21. import org.apache.activemq.core.remoting.impl.netty.NettyConnectorFactory;
  22.  
  23. public class Client
  24. {
  25.    
  26.     private static final String queueName = "queue.exampleQueue";
  27.     private static final String propName = "myprop";
  28.    
  29.     ClientSessionFactory sf = null;
  30.     Connection connection = null;
  31.     ClientSession session = null;
  32.     ClientProducer producer = null;
  33.     ClientMessage message = null;
  34.     ClientConsumer consumer = null;
  35.     Destination destinationQueue = null;
  36.    
  37.     public void initializeComponents(){
  38.        
  39.         try
  40.           {          
  41.             // Step 3. As we are not using a JNDI environment we instantiate the objects directly
  42.  
  43.              /**
  44.               * this map with configuration values is not necessary (it configures the default values).
  45.               * If you modify it to run the example in two different hosts, remember to also modify the
  46.               * server's Acceptor at {@link EmbeddedServer}
  47.               */
  48.              Map<String,Object> map = new HashMap<String,Object>();
  49.              map.put("host", "localhost");
  50.              map.put("port", 61616);
  51.              // -------------------------------------------------------
  52.  
  53.              ServerLocator serverLocator = ActiveMQClient.createServerLocatorWithoutHA(new TransportConfiguration(NettyConnectorFactory.class.getName(), map));
  54.              sf = serverLocator.createSessionFactory();
  55.  
  56.              // Step 4. Create a core queue
  57.              ClientSession coreSession = sf.createSession(false, false, false);
  58.              
  59.              try {
  60.                  coreSession.queueQuery(new SimpleString(queueName)).getName().toString();
  61.              } catch (NullPointerException e ) {
  62.                  coreSession.createQueue(queueName, queueName, false);
  63.              }
  64.                
  65.              coreSession.close();
  66.              
  67.             // Step 5. Create the session, and producer
  68.             session = sf.createSession();
  69.  
  70.             producer = session.createProducer(queueName); //
  71.  
  72.             // Step 6. Create and send a message
  73.             message = session.createMessage(false);
  74.             message.putStringProperty(propName, "Hello sent at " + new Date());
  75.  
  76.             // Step 7. Create the message consumer and start the connection
  77.             consumer = session.createConsumer(queueName);
  78.             session.start();
  79.  
  80.             // Step 8. Receive the message.
  81.             consumer.setMessageHandler(new MessageHandler(){
  82.                
  83.                 public void onMessage(ClientMessage message)
  84.                 {
  85.                     System.out.println("Received TextMessage:" + message.getStringProperty(propName));
  86.                 }
  87.                
  88.             });
  89.  
  90.           }
  91.           catch (Exception e)
  92.           {
  93.              e.printStackTrace();
  94.           }
  95.        
  96.     }
  97.    
  98.     public void sendMessage(){
  99.             try {
  100.                 System.out.println("Producer is going to send a message");
  101.                 producer.send(message);
  102.             } catch (ActiveMQException e) {
  103.                 // TODO Auto-generated catch block
  104.                 e.printStackTrace();
  105.             }
  106.              finally
  107.              {
  108.                 // Step 9. Be sure to close our resources!
  109.                 if (sf != null)
  110.                 {
  111.                    sf.close();
  112.                 }
  113.              }
  114.     }
  115.  
  116.    public static void main(final String[] args)
  117.    {
  118.        Client cl = new Client();
  119.        cl.initializeComponents();
  120.        cl.sendMessage();
  121.        
  122.    }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment