Biblioteca Java - Blame information for rev 31

Subversion Repositories:
Rev:
Rev Author Line No. Line
31 mihai 1 package test.clientserver;
2  
3 import org.apache.activemq.ActiveMQConnectionFactory;
4  
5 import javax.jms.*;
6 import java.util.Random;
7  
8 public class Client extends Thread implements MessageListener {
9     private static int ackMode;
10     private String clientQueueName;
11  
12     private String clientName;
13  
14     private boolean transacted = false;
15     private MessageProducer producer;
16     private Session session;
17     private Destination tempDest;
18  
19     static {
20         ackMode = Session.AUTO_ACKNOWLEDGE;
21     }
22  
23     public Client(String clientName) {
24         this.clientName = clientName;
25         this.clientQueueName = clientName;
26         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
27         Connection connection;
28         try {
29             connection = connectionFactory.createConnection();
30             connection.start();
31             session = connection.createSession(transacted, ackMode);
32             Destination adminQueue = session.createQueue(clientQueueName);
33  
34             //Setup a message producer to send message to the queue the server is consuming from
35             this.producer = session.createProducer(adminQueue);
36             this.producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
37  
38             //Create a temporary queue that this client will listen for responses on then create a consumer
39             //that consumes message from this temporary queue...for a real application a client should reuse
40             //the same temp queue for each message to the server...one temp queue per client
41             tempDest = session.createTemporaryQueue();
42             MessageConsumer responseConsumer = session.createConsumer(tempDest);
43  
44             //This class will handle the messages to the temp queue as well
45             responseConsumer.setMessageListener(this);
46  
47  
48         } catch (JMSException e) {
49             //Handle the exception appropriately
50             e.printStackTrace();
51         }
52     }
53  
54     public void sendMessage() {
55         try {
56             //Now create the actual message you want to send
57             TextMessage txtMessage = session.createTextMessage();
58             txtMessage.setText("MyProtocolMessage");
59  
60             //Set the reply to field to the temp queue you created above, this is the queue the server
61             //will respond to
62             txtMessage.setJMSReplyTo(tempDest);
63  
64             //Set a correlation ID so when you get a response you know which sent message the response is for
65             //If there is never more than one outstanding message to the server then the
66             //same correlation ID can be used for all the messages...if there is more than one outstanding
67             //message to the server you would presumably want to associate the correlation ID with this
68             //message somehow...a Map works good
69             String correlationId = this.createRandomString();
70             txtMessage.setJMSCorrelationID(correlationId);
71             System.out.println(":::" + txtMessage);
72             this.producer.send(txtMessage);
73         }catch(JMSException e){
74             e.printStackTrace();
75         }
76     }
77  
78  
79     private String createRandomString() {
80         Random random = new Random(System.currentTimeMillis());
81         long randomLong = random.nextLong();
82         return Long.toHexString(randomLong);
83     }
84  
85     public void onMessage(Message message) {
86         String messageText = null;
87         try {
88             if (message instanceof TextMessage) {
89                 TextMessage textMessage = (TextMessage) message;
90                 messageText = textMessage.getText();
91                 System.out.println("messageText = " + messageText + " with id "+message.getJMSMessageID());
92             }
93         } catch (JMSException e) {
94             e.printStackTrace();
95             //Handle the exception appropriately
96         }
97     }
98  
99     public void run(){
100         while(true) {
101             this.sendMessage();
102             try {
103                 Thread.sleep(1000);//one message/second
104             } catch (InterruptedException e) {
105                 e.printStackTrace();
106             }
107         }
108     }
109  
110     public static void main(String[] args) {
111  
112         Client c1 = new Client("client:echo");
113         c1.start();
114  
115         Client c2 = new Client("client:dummy");
116         c2.start();
117  
118         //Client c2 = new Client("client2:echo");
119         //c2.start();
120     }
121 }