티스토리 뷰
728x90
출처
- 3. Using Spring AMQP
- spring-rabbitmq-demo/spring-rabbitmq.xml at master · abel533/spring-rabbitmq-demo
- RabbitMQ와 Spring AMQP를 이용한 간단한 작업 큐 만들기|작성자 티몬개발
pom.xml
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-amqp</artifactId>
<version>2.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.1.12.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.3</version>
</dependency>
<!-- RabbitMQ Client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.8.0</version>
</dependency>
Spring AMQP 큐 메시지 송신
bluexmas-mq-rabbitmq.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit https://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- RabbitMQ 연결 설정 -->
<rabbit:connection-factory id="rabbitConnectionFactory"
virtual-host="/"
host="lastxmas.iptime.org"
port="5672"
channel-cache-size="10"
username="admin"
password="passwd"/>
<rabbit:admin connection-factory="rabbitConnectionFactory"/>
<!-- Queue 등록 -->
<rabbit:queue auto-declare="true" name="myQueue" />
<rabbit:direct-exchange name="amq.direct">
<rabbit:bindings>
<rabbit:binding key="foo.bar" queue="myQueue"/>
</rabbit:bindings>
</rabbit:direct-exchange>
<!-- 생산자와 소비자 모두에게 필요한 메시지 변환 -->
<bean id="amqpMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"/>
<!-- 메시지 리스너 설정 -->
<rabbit:listener-container connection-factory="rabbitConnectionFactory" message-converter="amqpMessageConverter">
<!-- 큐 myQueue 메시지는 bean id consumer의 method handleMessage가 처리한다 -->
<rabbit:listener queues="myQueue" ref="MQConsumer" method="handleMessage"/>
</rabbit:listener-container>
<bean id="MQConsumer" class="com.bluexmas.mq.MQConsumer"/>
</beans>
메시지 수신 클래스 ( MQConsumer.java )
package com.bluexmas.mq;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;
import com.bluexmas.dao.domain.CustomMessage;
import com.bluexmas.dao.domain.MPSMessage;
@Component
public class MQConsumer {
//메시지를 처리한다.
public void handleMessage(Message message) {
//do Something
System.out.println(message);
}
//메시지를 처리한다.
public void handleMessage(CustomMessage message) {
//do Something
System.out.println("--1> " + message);
}
}
Spring AMQP 큐 수동으로 전달
메시지 큐 전달 객체 ( CustomMessage.java )
package com.bluexmas.dao.domain;
public class CustomMessage {
private String text;
private int priority;
private boolean secret;
protected CustomMessage() {
}
public CustomMessage(String text, int priority, boolean secret) {
this.text = text;
this.priority = priority;
this.secret = secret;
}
public String getText() {
return text;
}
public int getPriority() {
return priority;
}
public boolean isSecret() {
return secret;
}
@Override
public String toString() {
return "CustomMessage{" + "text='" + text + '\'' + ", priority=" + priority + ", secret=" + secret + '}';
}
}
메시지 전달 객체 ( ProducerJSon2.java )
package com.bluexmas.test;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import com.bluexmas.dao.domain.CustomMessage;
import com.fasterxml.jackson.core.JsonProcessingException;
public class ProducerJSon2 {
public static void main(final String[] args) throws JsonProcessingException {
// RabbitMQ 연결
CachingConnectionFactory cf = new CachingConnectionFactory("lastxmas.iptime.org", 5672);
cf.setUsername("admin");
cf.setPassword("passwd");
// 메시지 보내기
RabbitTemplate template = new RabbitTemplate(cf);
template.setExchange("amq.direct");
template.setQueue("myQueue");
template.setMessageConverter(new Jackson2JsonMessageConverter());
CustomMessage customMessage = new CustomMessage("Hello Message!", 1, true);
template.convertAndSend("foo.bar", customMessage);
cf.destroy();
}
}
로그
00:29:34.276 [pool-1-thread-4] DEBUG org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - Storing delivery for consumerTag: 'amq.ctag-5t1nRdDJwg8A-H8jTH6DqQ' with deliveryTag: '1' in Consumer@1e292e89: tags=[[amq.ctag-5t1nRdDJwg8A-H8jTH6DqQ]], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@1.229.109.140:5672/,1), conn: Proxy@eefafec Shared Rabbit Connection: SimpleConnection@3680d2bb [delegate=amqp://admin@1.229.109.140:5672/, localPort= 4263], acknowledgeMode=AUTO local queue size=0
00:29:34.293 [org.springframework.amqp.rabbit.config.ListenerContainerFactoryBean#0-1] DEBUG org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - Received message: (Body:'{"text":"Hello Message!","priority":1,"secret":true}' MessageProperties [headers={__TypeId__=com.bluexmas.dao.domain.CustomMessage}, contentType=application/json, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=amq.direct, receivedRoutingKey=foo.bar, deliveryTag=1, consumerTag=amq.ctag-5t1nRdDJwg8A-H8jTH6DqQ, consumerQueue=myQueue])
--1> CustomMessage{text='Hello Message!', priority=1, secret=true}
댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- JavaScript
- ffmpeg
- 일본여행
- 서울오토살롱
- 동경
- ble
- 지스타2007
- Mac
- flex
- KOBA
- NDK
- Java
- Delphi Tip
- 튜닝쇼 2008
- Spring MVC
- ubuntu
- Linux
- Spring
- BPI-M4
- koba2010
- android
- sas2009
- Delphi
- 송주경
- 전예희
- MySQL
- 레이싱모델 익스트림 포토 페스티벌
- oracle
- Xcode
- SAS
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함