tobby48 5 роки тому
джерело
коміт
41b5ccf2a6

+ 7
- 0
pom.xml Переглянути файл

@@ -309,6 +309,13 @@
309 309
 			<artifactId>paranamer</artifactId>
310 310
 			<version>2.8</version>
311 311
 		</dependency>
312
+		
313
+		<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
314
+		<dependency>
315
+			<groupId>org.apache.activemq</groupId>
316
+			<artifactId>activemq-all</artifactId>
317
+			<version>5.11.0</version>
318
+		</dependency>
312 319
 	
313 320
 	</dependencies>
314 321
 	

+ 52
- 0
src/main/java/kr/co/swh/lecture/opensource/activemq/MessageReceiver.java Переглянути файл

@@ -0,0 +1,52 @@
1
+package kr.co.swh.lecture.opensource.activemq; 
2
+
3
+import javax.jms.Connection;
4
+import javax.jms.ConnectionFactory;
5
+import javax.jms.Destination;
6
+import javax.jms.JMSException;
7
+import javax.jms.Message;
8
+import javax.jms.MessageConsumer;
9
+import javax.jms.Session;
10
+import javax.jms.TextMessage;
11
+ 
12
+import org.apache.activemq.ActiveMQConnection;
13
+import org.apache.activemq.ActiveMQConnectionFactory;
14
+ 
15
+public class MessageReceiver {
16
+ 
17
+    // URL of the JMS server
18
+//    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
19
+    private static String url = "tcp://192.168.0.14:61616";
20
+    // default broker URL is : tcp://localhost:61616"
21
+ 
22
+    // Name of the queue we will receive messages from
23
+    private static String subject = "JCG_QUEUE";
24
+ 
25
+    public static void main(String[] args) throws JMSException {
26
+        // Getting JMS connection from the server
27
+        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
28
+        Connection connection = connectionFactory.createConnection();
29
+        connection.start();
30
+ 
31
+        // Creating session for seding messages
32
+        Session session = connection.createSession(false,
33
+                Session.AUTO_ACKNOWLEDGE);
34
+ 
35
+        // Getting the queue 'JCG_QUEUE'
36
+        Destination destination = session.createQueue(subject);
37
+ 
38
+        // MessageConsumer is used for receiving (consuming) messages
39
+        MessageConsumer consumer = session.createConsumer(destination);
40
+ 
41
+        // Here we receive the message.
42
+        Message message = consumer.receive();
43
+ 
44
+        // We will be using TestMessage in our example. MessageProducer sent us a TextMessage
45
+        // so we must cast to it to get access to its .getText() method.
46
+        if (message instanceof TextMessage) {
47
+            TextMessage textMessage = (TextMessage) message;
48
+            System.out.println("Received message '" + textMessage.getText() + "'");
49
+        }
50
+        connection.close();
51
+    }
52
+}

+ 61
- 0
src/main/java/kr/co/swh/lecture/opensource/activemq/MessageSender.java Переглянути файл

@@ -0,0 +1,61 @@
1
+package kr.co.swh.lecture.opensource.activemq; 
2
+
3
+import javax.jms.Connection;
4
+import javax.jms.ConnectionFactory;
5
+import javax.jms.Destination;
6
+import javax.jms.JMSException;
7
+import javax.jms.MessageProducer;
8
+import javax.jms.Session;
9
+import javax.jms.TextMessage;
10
+
11
+import org.apache.activemq.ActiveMQConnection;
12
+import org.apache.activemq.ActiveMQConnectionFactory;
13
+/**
14
+ * <pre>
15
+ * kr.co.swh.lecture.opensource.activemq 
16
+ * MessageSender.java
17
+ *
18
+ * 설명 :
19
+ * </pre>
20
+ * 
21
+ * @since : 2020. 6. 14.
22
+ * @author : tobby48
23
+ * @version : v1.0
24
+ */
25
+public class MessageSender {
26
+
27
+	//URL of the JMS server. DEFAULT_BROKER_URL will just mean that JMS server is on localhost
28
+//	private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
29
+	private static String url = "tcp://192.168.0.14:61616";
30
+	
31
+	// default broker URL is : tcp://localhost:61616"
32
+	private static String subject = "JCG_QUEUE"; // Queue Name.You can create any/many queue names as per your requirement. 
33
+
34
+	public static void main(String[] args) throws JMSException {        
35
+		// Getting JMS connection from the server and starting it
36
+		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
37
+		Connection connection = connectionFactory.createConnection();
38
+		connection.start();
39
+
40
+		//Creating a non transactional session to send/receive JMS message.
41
+		Session session = connection.createSession(false,
42
+				Session.AUTO_ACKNOWLEDGE);
43
+
44
+		//Destination represents here our queue 'JCG_QUEUE' on the JMS server. 
45
+		//The queue will be created automatically on the server.
46
+		Destination destination = session.createQueue(subject); 
47
+
48
+		// MessageProducer is used for sending messages to the queue.
49
+		MessageProducer producer = session.createProducer(destination);
50
+
51
+		// We will send a small text message saying 'Hello World!!!' 
52
+		TextMessage message = session
53
+				.createTextMessage("Hello !!! Welcome to the world of ActiveMQ.");
54
+
55
+		// Here we are sending our message!
56
+		producer.send(message);
57
+
58
+		System.out.println("JCG printing@@ '" + message.getText() + "'");
59
+		connection.close();
60
+	}
61
+}

+ 29
- 1
src/main/java/kr/co/swh/lecture/opensource/rabbitmq/Publisher.java Переглянути файл

@@ -7,17 +7,45 @@ import com.rabbitmq.client.Channel;
7 7
 import com.rabbitmq.client.Connection;
8 8
 import com.rabbitmq.client.ConnectionFactory;
9 9
 
10
+/**
11
+ * <pre>
12
+ * kr.co.swh.lecture.opensource.rabbitmq 
13
+ * Publisher.java
14
+ *
15
+ * 설명 :
16
+ * </pre>
17
+ * 
18
+ * @since : 2020. 6. 14.
19
+ * @author : tobby48
20
+ * @version : v1.0
21
+ */
10 22
 public class Publisher {
11 23
 
12 24
 	private final static String QUEUE_NAME = "hello";
13 25
 
26
+	/**
27
+	 * <pre>
28
+	 * 설명 : 
29
+	 * 변경이력 : 
30
+	 * -----------------------------------------------------------------
31
+	 * 변경일       작성자       변경내용  
32
+	 * ------------ ------------ ---------------------------------------
33
+	 * 2020. 6. 14. tobby48          최초 작성 
34
+	 * -----------------------------------------------------------------
35
+	 * </pre> 
36
+	 * @since : 2020. 6. 14.
37
+	 * @author : tobby48
38
+	 * @param args
39
+	 * @throws IOException
40
+	 * @throws TimeoutException
41
+	 */ 	
14 42
 	public static void main(String[] args) throws IOException, TimeoutException {
15 43
 		ConnectionFactory factory = new ConnectionFactory();
16 44
 		factory.setHost("dev-swh.ga");
17 45
 		Connection connection = factory.newConnection();
18 46
 		Channel channel = connection.createChannel();
19
-
20 47
 		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
48
+		
21 49
 		String message = "Hello World!";
22 50
 		channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
23 51
 		System.out.println(" [x] Sent '" + message + "'");

+ 13
- 0
src/main/java/kr/co/swh/lecture/opensource/rabbitmq/Receiver.java Переглянути файл

@@ -9,6 +9,18 @@ import com.rabbitmq.client.ConnectionFactory;
9 9
 import com.rabbitmq.client.Consumer;
10 10
 import com.rabbitmq.client.DefaultConsumer;
11 11
 import com.rabbitmq.client.Envelope;
12
+/**
13
+ * <pre>
14
+ * kr.co.swh.lecture.opensource.rabbitmq 
15
+ * Receiver.java
16
+ *
17
+ * 설명 :
18
+ * </pre>
19
+ * 
20
+ * @since : 2020. 6. 14.
21
+ * @author : tobby48
22
+ * @version : v1.0
23
+ */
12 24
 public class Receiver {
13 25
 	private final static String QUEUE_NAME = "hello";
14 26
 	public static void main(String[] argv) throws IOException, TimeoutException {
@@ -17,6 +29,7 @@ public class Receiver {
17 29
 		Connection connection = factory.newConnection();
18 30
 		Channel channel = connection.createChannel();
19 31
 		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
32
+		
20 33
 		System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
21 34
 		Consumer consumer = new DefaultConsumer(channel) {
22 35
 			@Override