Biblioteca Java - Blame information for rev 31

Subversion Repositories:
Rev:
Rev Author Line No. Line
31 mihai 1 package test.clientserver;
2 import org.apache.activemq.broker.BrokerService;
3 import org.apache.activemq.ActiveMQConnectionFactory;
4  
5 import javax.jms.*;
6  
7 public class Server implements MessageListener {
8     private static int ackMode;
9     //private static String messageQueueName;
10     private static String messageBrokerUrl;
11  
12     private Session session;
13     private boolean transacted = false;
14     private MessageProducer replyProducer;
15     private MessageProtocol messageProtocol;
16     Connection connection;
17  
18     static {
19         messageBrokerUrl = "failover:tcp://localhost:61616";
20         //messageQueueName = "client.messages";
21         ackMode = Session.AUTO_ACKNOWLEDGE;
22     }
23  
24     public Server() throws JMSException {
25         try {
26             //This message broker is embedded
27             BrokerService broker = new BrokerService();
28             broker.setPersistent(false);
29             broker.setUseJmx(false);
30             broker.addConnector(messageBrokerUrl);
31             broker.start();
32         } catch (Exception e) {
33             //Handle the exception appropriately
34         }
35  
36         //Delegating the handling of messages to another class, instantiate it before setting up JMS so it
37         //is ready to handle messages
38         this.messageProtocol = new MessageProtocol();
39  
40         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(messageBrokerUrl);
41         connection = connectionFactory.createConnection();
42         connection.start();
43         this.session = connection.createSession(this.transacted, ackMode);
44  
45         setupMessageQueueConsumer("client:echo");
46         //setupMessageQueueConsumer("Client 2.messages");
47     }
48  
49     private void setupMessageQueueConsumer(String messageQueueName) {
50  
51         try {
52  
53             Destination adminQueue = this.session.createQueue(messageQueueName);
54  
55             //Setup a message producer to respond to messages from clients, we will get the destination
56             //to send to from the JMSReplyTo header field from a Message
57             this.replyProducer = this.session.createProducer(null);
58             this.replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
59  
60             //Set up a consumer to consume messages off of the admin queue
61             MessageConsumer consumer = this.session.createConsumer(adminQueue);
62             consumer.setMessageListener(this);
63  
64  
65         } catch (JMSException e) {
66             //Handle the exception appropriately
67             e.printStackTrace();
68         }
69     }
70  
71     public void onMessage(Message message) {
72         try {
73             TextMessage response = this.session.createTextMessage();
74             if (message instanceof TextMessage) {
75                 TextMessage txtMsg = (TextMessage) message;
76                 String messageText = txtMsg.getText();
77                 response.setText(this.messageProtocol.handleProtocolMessage(messageText));
78             }
79  
80             //Set the correlation ID from the received message to be the correlation id of the response message
81             //this lets the client identify which message this is a response to if it has more than
82             //one outstanding message to the server
83             response.setJMSCorrelationID(message.getJMSCorrelationID());
84  
85             //Send the response to the Destination specified by the JMSReplyTo field of the received message,
86             //this is presumably a temporary queue created by the client
87             this.replyProducer.send(message.getJMSReplyTo(), response);
88         } catch (JMSException e) {
89             //Handle the exception appropriately
90             e.printStackTrace();
91         }
92     }
93  
94     public static void main(String[] args) throws JMSException {
95         new Server();
96     }
97 }