ActiveMQ는 JMS API를 구현하는 메시지 브로커로, 여러 교차 언어 클라이언트와 네트워크 프로토콜을 지원합니다.
JMS API
Producer ----> Broker ----> Consumer
위의 단계로 메세지를 전송한다.
(주의 : socket, inputStream, outputStream 이런 단어를 쓰면 안됨)
Producer에서 데이터를 Broker(우체통 역할)에 전달하면 Consumer는 원할 때 Broker에서 메세지를 받아갈 수 있다.
따라서 Producer와 consumer의 연결은 끊기더라도 상관 없다는 점에서 소켓과 다르다.
JAVA 8버전 기준
ActiveMQ실행 방법
ActiveMQ
Apache ActiveMQ™ is the most popular open source, multi-protocol, Java-based messaging server. It supports industry standard protocols so users get the benefits of client choices across a broad range of languages and platforms. Connectivity from C, C++,
activemq.apache.org
사이트에 들어가서 다운로드 받습니다. classic을 다운 받습니다.
위의 명령어로 브로커를 실행한다.
JRE인가 JDK의 라이브러리가 겹쳐서 충돌하는 경우가 있으니 조심....
Project 오른쪽 클릭 -> properties ->java build Path -> Add external JARs ->
위의 라이브러리들을 추가
ActiveMQExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package activeMQ;
public class ActiveMQExample {
private JmsMessageConsumer messageConsumer;
private JmsMessageProducer messageProducer;
public ActiveMQExample() {
messageProducer = new JmsMessageProducer();
messageConsumer = new JmsMessageConsumer();
messageProducer.start();
start();
}
private void start() {
while(true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ActiveMQExample();
}
}
|
cs |
JmsMessageConsumer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package activeMQ;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JmsMessageConsumer implements MessageListener{
public JmsMessageConsumer() {
init();
}
private void init() {
try {
//Create connectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
//Create a connection
Connection connection = connectionFactory.createConnection();
connection.start();
//Create Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//Create Destination
Destination destination = session.createQueue("destinationName");
//Create messageProducer from the session to the queue
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(this);
}catch (Exception e) {
System.out.println("\nerror:\n");
e.printStackTrace();
}
}
@Override
public void onMessage(Message message) {
try {
System.out.println("Received messages = " + ((TextMessage) message).getText());
}catch (Exception e) {
e.printStackTrace();
}
}
}
|
cs |
JmsMessageProducer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
package activeMQ;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JmsMessageProducer extends Thread{
@Override
public void run() {
super.run();
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
String sendingMessage = null;
BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
while(true) {
try {
// crate connection
Connection connection = connectionFactory.createConnection();
connection.start();
//create a session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//Create destination
Destination destination = session.createQueue("destinationName");
//Create a MessageProducer from the session to the queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); //반복되는
//Create message
sendingMessage = in2.readLine();
if(sendingMessage==null)
continue;
if(sendingMessage=="exit")
break;
TextMessage message = session.createTextMessage(sendingMessage);
//Tell the producer to send the message
producer.send(message);
System.out.println("SendMessage : " + message.getText());
session.close();
connection.close();
}catch (Exception e) {
System.out.println("run error");
e.printStackTrace();
}
}
}
}
|
cs |
출처 :
activemq.apache.org/hello-world
ActiveMQ
Apache ActiveMQ, ActiveMQ, ActiveMQ Artemis, Apache, the Apache feather logo, and the Apache ActiveMQ project logo are trademarks of The Apache Software Foundation. Copyright © 2019, The Apache Software Foundation. Licensed under Apache License 2.0.
activemq.apache.org
'Java > 통신' 카테고리의 다른 글
JAXB 통신 example (0) | 2021.01.21 |
---|---|
공굴리기 통신 (socket통신) (0) | 2021.01.14 |