Biblioteca Java - Blame information for rev 31

Subversion Repositories:
Rev:
Rev Author Line No. Line
31 mihai 1 package com.linkscreens.activemq.server;
2  
3 import com.linkscreens.activemq.protocol.ProtocolType;
4 import org.apache.activemq.ActiveMQConnectionFactory;
5  
6 import javax.jms.*;
7 import java.util.ArrayList;
8  
9 /**
10  * Created by evo2 on 7/29/2015.
11  */
12 public class Server implements ExceptionListener {
13     private static int ackMode;
14     private static String messageBrokerUrl;
15     private Session session;
16     private boolean transacted = false;
17     private MessageProducer replyProducer;
18     private Connection connection;
19     private ArrayList<ServerMessageHandler> handlers = new ArrayList<ServerMessageHandler>();
20  
21     static {
22         messageBrokerUrl = "tcp://localhost:61616";
23         ackMode = Session.AUTO_ACKNOWLEDGE;
24     }
25  
26     public Server() throws JMSException {
27       System.out.println("Starting ActiveMQ server.");
28         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(messageBrokerUrl);
29         connection = connectionFactory.createConnection();
30         connection.start();
31         this.session = connection.createSession(this.transacted, ackMode);
32     }
33  
34     public void addMessageHandler(ProtocolType protocol) {
35  
36         try {
37  
38             Destination adminQueue = this.session.createQueue(protocol.toString());
39  
40             //Setup a message producer to respond to messages from clients, we will get the destination
41             //to send to from the JMSReplyTo header field from a Message
42  
43             //reuse the same MessageProducer for sending messages to different destinations; just create it with a null destination and specify it on the send method.
44             this.replyProducer = this.session.createProducer(null);
45             this.replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
46  
47             //Set up a consumer to consume messages off of the admin queue
48             MessageConsumer consumer = this.session.createConsumer(adminQueue);
49             ServerMessageHandler handler = new ServerMessageHandler(protocol, session, replyProducer);
50             consumer.setMessageListener(handler);
51             handlers.add(handler);
52             System.out.println("Handler added for protocol "+protocol.toString());
53         } catch (JMSException e) {
54             //Handle the exception appropriately
55             e.printStackTrace();
56         }
57  
58     }
59  
60     public void shutdown(){
61         try {
62             session.close();
63             connection.close();
64         } catch (JMSException e) {
65             e.printStackTrace();
66         }
67  
68     }
69  
70     /**
71      * Handling JMS exceptions.
72      * @param e
73      */
74     public synchronized void onException(JMSException e) {
75         System.out.println("JMS exception has occurred.");
76         if(e!=null)
77             e.printStackTrace();
78     }
79  
80     public static void main(String[] args) throws JMSException {
81         Server server = new Server();
82         server.addMessageHandler(ProtocolType.ECHO);
83         server.addMessageHandler(ProtocolType.DUMMY);
84     }
85  
86 }
87