RabbitMQ 는 생산자 와 소비 자 를 배치 합 니 다.

기본적으로 RabbitMQ 서비스 가 설치 되 어 있 으 며,RabbitMQ 관리 단 페이지(기본 port:15672)에 exchange,queue 및 exchange 와 queue 의 연결 관계 등 을 만들어 야 합 니 다.
다음은 프로젝트 중의 생산자 와 소비자 의 배 치 를 진행한다.
의존 추가
프로젝트 파일 pom.xml 에 의존 도 를 추가 합 니 다.
<!-- RabbitMQ  spring -->
<!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit -->

<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
    <version>2.2.7.RELEASE</version>
</dependency>

생산자
1.설정
<?xml version="1.0" encoding="UTF-8"?>
<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/rabbit
        http://www.springframework.org/schema/rabbit/spring-rabbit-2.2.xsd">

    <!--         -->


    <!-- rabbit   -->
    <rabbit:connection-factory id="connectionFactory"
                                username="guest"
                                password="guest"
                                host="192.168.8.130"
                                port="5672"
                                channel-cache-size="50"
                                publisher-returns="true"
                                publisher-confirms="true"
                                connection-timeout="5000"
                                />

    <!--        admin  ,  producer  exchange queue  rabbitmq         -->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--        -->
    <bean id="jsonMessageConverter"  class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"/>

    <!--             -->
    <rabbit:template id="rabbitProviderName" exchange="exchange-name"  connection-factory="connectionFactory" message-converter="jsonMessageConverter" />

    <!--      -->
    <rabbit:queue id="queue-id" durable="true" auto-delete="false" exclusive="false" name="queues-name" />
    <!-- durable:     ; exclusive:; auto_delete:-->    

</beans>

2.코드-생산자 생산 소식
package com.api;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
//   import

public class ConsumerHandlerClass 
{
    @Autowired  
    private RabbitTemplate rabbitProviderName; //rabbitProviderName       template id  

    public void sendMessage(){
        Message message = new Message(("{\"key\":1}").getBytes(), new MessageProperties());
        rabbitProviderName.send(message);
    }
}

소비자
1.설정
<?xml version="1.0" encoding="UTF-8"?>
<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/rabbit
        http://www.springframework.org/schema/rabbit/spring-rabbit-2.2.xsd">
    

    <!--        -->

    <!-- rabbit   -->
    <rabbit:connection-factory id="connectionFactory"
                                username="guest"
                                password="guest"
                                host="192.168.8.130"
                                port="5672"
                                channel-cache-size="50"
                                publisher-returns="true"
                                publisher-confirms="true"
                                connection-timeout="5000"
                                />

    <!--        admin  ,  producer  exchange queue  rabbitmq         -->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--                   json      -->
    <bean id="jsonMessageConverter"  class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"/>

    <!--             -->
    <rabbit:template id="consumerTemplate" connection-factory="connectionFactory" exchange="exchange-name" message-converter="jsonMessageConverter"/>

    <!-- topic -->
    <rabbit:topic-exchange name="exchange-name" durable="true" auto-delete="false">
        <rabbit:bindings>
            <rabbit:binding queue="queue-id" pattern="queues-name.*"/>
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!--             -->
    <bean id="consumerHandler" class="com.api.ConsumerHandlerClass"/>

    <!--               -->
    <bean id="receiveListenerAdapter" class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
        <constructor-arg ref="consumerHandler" />
        <property name="defaultListenerMethod" value="handlerFunc"></property> <!--          ,            -->
        <property name="messageConverter" ref="jsonMessageConverter"></property>
    </bean>

    <!--      -->
    <rabbit:listener-container connection-factory="connectionFactory">
        <rabbit:listener queues="queue-id" ref="receiveListenerAdapter"/>
        <!--             <rabbit:listener queues="queue-id2" ref="receiveListenerAdapter2"/>-->
    </rabbit:listener-container>

</beans>

2.코드-소비자 처리 메시지
package com.api;

//   import

public class ConsumerHandlerClass 
{
    public void handlerFunc(HashMap arguments) {
        //arguments          
    }
}

좋은 웹페이지 즐겨찾기