Guest User

Untitled

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