Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/panacya/platform/service/bus/client/JmsSimpleClient.java


1   package com.panacya.platform.service.bus.client;
2   
3   import org.activemq.ActiveMQConnectionFactory;
4   import org.springframework.jms.core.JmsTemplate;
5   import org.springframework.jms.core.MessageCreator;
6   
7   import javax.jms.Message;
8   import javax.jms.Session;
9   import javax.jms.JMSException;
10  import javax.naming.InitialContext;
11  import javax.naming.Context;
12  import javax.naming.NamingException;
13  import javax.jms.ConnectionFactory;
14  
15  import org.activemq.util.IdGenerator;
16  
17  import java.util.Properties;
18  import java.lang.reflect.InvocationTargetException;
19  
20  /**
21   * @author <a href="mailto:michael.gaffney@panacya.com">Michael Gaffney </a>
22   */
23  public class JmsSimpleClient {
24  
25      private static final String BROKER_URL = "tcp://localhost:61616";
26      //private static final String BROKER_URL = "jnp://localhost:1099";
27      private static final String SEND_CMD = "send";
28      private static final String RECEIVE_CMD = "receive";
29      private static final String ENDLESS_RECEIVE_CMD = "receive-non-stop";
30      private static final String SEND_RECEIVE_CMD = "send-receive";
31  
32      public static final String NAMING_CONTEXT = "org.jnp.interfaces.NamingContextFactory";
33      public static final String JNP_INTERFACES = "org.jnp.interfaces";
34  
35  
36      public static void main(String[] args) {
37          execute(new ClientArgs(args));
38      }
39  
40      private static void execute(ClientArgs args) {
41          try {
42              if (SEND_CMD.equals(args.getCommand())) {
43                  JmsSimpleClient.sendToActiveMQ(args.getDestination(), args.getNoOfMessages());
44              } else if (RECEIVE_CMD.equals(args.getCommand())) {
45                  JmsSimpleClient.receiveFromActiveMQ(args.getDestination(), args.getTimeout());
46              } else if (ENDLESS_RECEIVE_CMD.equals(args.getCommand())) {
47                  JmsSimpleClient.receiveFromActiveMQ(args.getDestination());
48              } else if (SEND_RECEIVE_CMD.equals(args.getCommand())) {
49                  JmsSimpleClient.sendToActiveMQ(args.getDestination(), args.getNoOfMessages());
50                  JmsSimpleClient.receiveFromActiveMQ(args.getDestination(), args.getTimeout());
51              } else {
52                  System.err.println("Unknown command: " + args.getCommand());
53                  System.exit(-1);
54              }
55          } catch (Exception e) {
56              e.printStackTrace();
57          }
58      }
59  
60      public static void sendToActiveMQ(String destinationName, int count) throws Exception {
61          System.out.println("Sending to '" + destinationName + "' ...");
62          JmsTemplate jt = createTemplate(destinationName);
63  
64          int i = 0;
65          for (; i < count; i++) {
66              jt.send(destinationName, new MessageCreator() {
67                  public Message createMessage(Session session) throws JMSException {
68                      return session.createTextMessage("hello ActiveMQ world ");
69                  }
70              });
71          }
72  
73          System.out.println("Done sending " + count + " message/s ........");
74      }
75  
76      public static void receiveFromActiveMQ(String destinationName, long timeout) throws Exception {
77          System.out.println("Listening to '" + destinationName + "' ...");
78          JmsTemplate jt = createTemplate(destinationName);
79          jt.setReceiveTimeout(timeout);
80          while (true) {
81              Message aMessage = jt.receive(destinationName);
82              System.out.println("...done");
83              if (aMessage == null) {
84                  System.out.println("No message received");
85              } else {
86                  System.out.println("Message Received: " + aMessage.toString());
87              }
88          }
89      }
90  
91      public static void receiveFromActiveMQ(String destinationName) throws Exception {
92          System.out.println("Listening to '" + destinationName + "' ...");
93          JmsTemplate jt = createTemplate(destinationName);
94  
95          while (true) {
96              Message aMessage = jt.receive(destinationName);
97              if (aMessage == null) {
98                  System.out.println("No message received");
99              } else {
100                 int messageNumber = aMessage.getIntProperty("MessageNumber");
101                 System.out.println("Received MessageNumber: " + messageNumber);
102             }
103         }
104     }
105 
106     private static JmsTemplate createTemplate(String destinationName) {
107 
108         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
109         connectionFactory.setBrokerURL(BROKER_URL);
110 
111         IdGenerator idGenerator = new IdGenerator();
112         connectionFactory.setClientID(idGenerator.generateId());
113 
114         JmsTemplate jt = new JmsTemplate(connectionFactory);
115         if (destinationName.startsWith("topic")) {
116             jt.setPubSubDomain(true);
117         }
118 
119         return jt;
120 
121     }
122 
123 
124 }