Spring + Rabbit MQ

북마크 추가

Rabbit MQ 설치 : http://trandent.com/board/etc/detail/603 

Rabbit MQ 방식설명 : http://trandent.com/board/etc/detail/604

 

Spring + Rabbit MQ Topics 방식 예제

 

1.pom.xml dependency 추가

 

<dependency>

    <groupId>org.springframework.amqp</groupId>

    <artifactId>spring-amqp</artifactId>

    <version>1.1.4.RELEASE</version>

</dependency>

<dependency>

    <groupId>org.springframework.amqp</groupId>

    <artifactId>spring-rabbit</artifactId>

    <version>1.1.1.RELEASE</version>

</dependency>

 

2.resources 폴더에 Rabbit-sender-context.xml 파일 생성

 

<?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.1.xsd

    http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">

    <!--  first following line creates a rabbit connection factory with specified parameters -->

    <rabbit:connection-factory id="connectionFactory" host="localhost" username="guest" password="guest" />

    <!-- obtain admin rights to create the an exchange -->

    <rabbit:admin connection-factory="connectionFactory" />

 

    <!-- create a bean which can send message to TUTORIAL_EXCHANGE for the Java program to call -->

    <rabbit:template id="tutorialTemplate" connection-factory="connectionFactory"  exchange="TUTORIAL-EXCHANGE"/>

</beans>

 

 

3. listener 클래스 생성

 

 

import org.springframework.amqp.core.Message;

import org.springframework.amqp.core.MessageListener;

 

public class TutorialListener implements MessageListener {

    public void onMessage(Message message) {

    String messageBody= new String(message.getBody());

        System.out.println("Listener received message----->"+messageBody);

    }

}

 

4.resources 폴더에 Rabbit-listener-context.xml 파일 생성

  

<? 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.1.xsd

http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">

    <rabbit:connection-factory id="connectionFactory" host="localhost" username="guest" password="guest" />

    <rabbit:admin connection-factory="connectionFactory" />

    <!-- Create myAnonymousQueue queue -->

    <rabbit:queue id="myAnonymousQueue" />

    <!-- create myExchange and bind myAnonymousQueue with my.routingkey.1 to the TUTORIAL-EXCHANGE-->

    <rabbit:topic-exchange id="myExchange" name="TUTORIAL-EXCHANGE">

        <rabbit:bindings>

            <rabbit:binding queue="myAnonymousQueue" pattern="my.#.*"></rabbit:binding>

        </rabbit:bindings>

    </rabbit:topic-exchange>

    <!-- instantiate TutorialListener -->

    <bean id="aListener" class="com.keyhole.amqp.TutorialListener" />

    <!-- glue the listener and myAnonymousQueue to the container-->

    <rabbit:listener-container id="myListenerContainer" connection-factory="connectionFactory">

        <rabbit:listener ref="aListener" queues="myAnonymousQueue" /></rabbit:listener-container>

</ beans>

 

 

5.Listener container를 생성하여 listner load

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class TutorialListenerContainer {

    public static void main(String[] args) {

        ApplicationContext c1 = new ClassPathXmlApplicationContext("Rabbt-listener-contet.xml");

 

    }

}

 

6.Message Sender 생성

 

 

public class TutorialSender {

    public static void main(String[] args) throws Exception {

        ApplicationContext context = new ClassPathXmlApplicationContext("rabbit-sender-context.xml");//loading beans

        AmqpTemplate aTemplate = (AmqpTemplate) context.getBean("tutorialTemplate");// getting a reference to the sender bean

 

        for (int i = 0; i < 10; i++)

            aTemplate.convertAndSend("my.routingkey.1", "Message # " +i +" on "+ new Date());// send

    }

}

//end of TutorialSender

 

7. Listener Container와 Sender를 java application으로 실행한다.

 

 

My.routingkey.1로 지정되어 있는 패턴을 my.#.*로 변경해서 실행 할 수도 있다. 

 

 

 

 

 

AD
통관알리미
2014-09-29 15:38
SHARE