Biblioteca Java - Blame information for rev 31
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
31 | mihai | 1 | package test.activemq; |
2 | |||
3 | import org.apache.activemq.ActiveMQConnectionFactory; | ||
4 | |||
5 | import javax.jms.Connection; | ||
6 | import javax.jms.DeliveryMode; | ||
7 | import javax.jms.Destination; | ||
8 | import javax.jms.ExceptionListener; | ||
9 | import javax.jms.JMSException; | ||
10 | import javax.jms.Message; | ||
11 | import javax.jms.MessageConsumer; | ||
12 | import javax.jms.MessageProducer; | ||
13 | import javax.jms.Session; | ||
14 | import javax.jms.TextMessage; | ||
15 | |||
16 | /** | ||
17 | * Hello world! | ||
18 | */ | ||
19 | public class App { | ||
20 | |||
21 | public static void main(String[] args) throws Exception { | ||
22 | |||
23 | for(int i=0;i<40;i++) | ||
24 | thread(new HelloWorldConsumer(), false); | ||
25 | |||
26 | /*thread(new HelloWorldProducer(), false); | ||
27 | thread(new HelloWorldProducer(), false); | ||
28 | thread(new HelloWorldConsumer(), false); | ||
29 | Thread.sleep(1000); | ||
30 | thread(new HelloWorldConsumer(), false); | ||
31 | thread(new HelloWorldProducer(), false); | ||
32 | thread(new HelloWorldConsumer(), false); | ||
33 | thread(new HelloWorldProducer(), false); | ||
34 | Thread.sleep(1000); | ||
35 | thread(new HelloWorldConsumer(), false); | ||
36 | thread(new HelloWorldProducer(), false); | ||
37 | thread(new HelloWorldConsumer(), false); | ||
38 | thread(new HelloWorldConsumer(), false); | ||
39 | thread(new HelloWorldProducer(), false); | ||
40 | thread(new HelloWorldProducer(), false); | ||
41 | Thread.sleep(1000); | ||
42 | thread(new HelloWorldProducer(), false); | ||
43 | thread(new HelloWorldConsumer(), false); | ||
44 | thread(new HelloWorldConsumer(), false); | ||
45 | thread(new HelloWorldProducer(), false); | ||
46 | thread(new HelloWorldConsumer(), false); | ||
47 | thread(new HelloWorldProducer(), false); | ||
48 | thread(new HelloWorldConsumer(), false); | ||
49 | thread(new HelloWorldProducer(), false); | ||
50 | thread(new HelloWorldConsumer(), false); | ||
51 | thread(new HelloWorldConsumer(), false); | ||
52 | thread(new HelloWorldProducer(), false); | ||
53 | */ | ||
54 | } | ||
55 | |||
56 | public static void thread(Runnable runnable, boolean daemon) { | ||
57 | Thread brokerThread = new Thread(runnable); | ||
58 | brokerThread.setDaemon(daemon); | ||
59 | brokerThread.start(); | ||
60 | } | ||
61 | |||
62 | public static class HelloWorldProducer implements Runnable { | ||
63 | public void run() { | ||
64 | try { | ||
65 | // Create a ConnectionFactory | ||
66 | //ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost"); | ||
67 | ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); | ||
68 | |||
69 | // Create a Connection | ||
70 | Connection connection = connectionFactory.createConnection(); | ||
71 | connection.start(); | ||
72 | |||
73 | // Create a Session | ||
74 | Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | ||
75 | |||
76 | // Create the destination (Topic or Queue) | ||
77 | Destination destination = session.createQueue("TEST.FOO"); | ||
78 | |||
79 | // Create a MessageProducer from the Session to the Topic or Queue | ||
80 | MessageProducer producer = session.createProducer(destination); | ||
81 | producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); | ||
82 | |||
83 | // Create a messages | ||
84 | String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode(); | ||
85 | TextMessage message = session.createTextMessage(text); | ||
86 | |||
87 | // Tell the producer to send the message | ||
88 | System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName()); | ||
89 | producer.send(message); | ||
90 | |||
91 | // Clean up | ||
92 | session.close(); | ||
93 | connection.close(); | ||
94 | } | ||
95 | catch (Exception e) { | ||
96 | System.out.println("Caught: " + e); | ||
97 | e.printStackTrace(); | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | |||
102 | public static class HelloWorldConsumer implements Runnable, ExceptionListener { | ||
103 | public void run() { | ||
104 | try { | ||
105 | |||
106 | // Create a ConnectionFactory | ||
107 | ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); | ||
108 | |||
109 | // Create a Connection | ||
110 | Connection connection = connectionFactory.createConnection(); | ||
111 | connection.start(); | ||
112 | |||
113 | connection.setExceptionListener(this); | ||
114 | |||
115 | // Create a Session | ||
116 | Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | ||
117 | |||
118 | // Create the destination (Topic or Queue) | ||
119 | Destination destination = session.createQueue("TEST.FOO"); | ||
120 | |||
121 | // Create a MessageConsumer from the Session to the Topic or Queue | ||
122 | MessageConsumer consumer = session.createConsumer(destination); | ||
123 | |||
124 | // Wait for a message | ||
125 | Message message = consumer.receive(1000); | ||
126 | |||
127 | if (message instanceof TextMessage) { | ||
128 | TextMessage textMessage = (TextMessage) message; | ||
129 | String text = textMessage.getText(); | ||
130 | System.out.println("Received: " + text); | ||
131 | } else { | ||
132 | System.out.println("Received: " + message); | ||
133 | } | ||
134 | |||
135 | consumer.close(); | ||
136 | session.close(); | ||
137 | connection.close(); | ||
138 | } catch (Exception e) { | ||
139 | System.out.println("Caught: " + e); | ||
140 | e.printStackTrace(); | ||
141 | } | ||
142 | } | ||
143 | |||
144 | public synchronized void onException(JMSException ex) { | ||
145 | System.out.println("JMS Exception occured. Shutting down client."); | ||
146 | } | ||
147 | } | ||
148 | } |