클라우드 컴퓨팅 & NoSQL/RabbitMq

RabbitMQ + Spring

Terry Cho 2013. 8. 27. 22:51

pom.xml

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

 

  <groupId>com.terry</groupId>

  <artifactId>rabbitmq</artifactId>

  <version>1.0-SNAPSHOT</version>

  <packaging>jar</packaging>

 

  <name>rabbitmq</name>

  <url>http://maven.apache.org</url>

  <repositories>

         <repository>

             <id>spring-release</id>

             <name>Spring Maven Release Repository</name>

             <url>http://repo.springsource.org/libs-release</url>

         </repository>

  </repositories>

 

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  </properties>

 

  <dependencies>

         <dependency>

           <groupId>com.rabbitmq</groupId>

           <artifactId>amqp-client</artifactId>

           <version>3.1.4</version>

         </dependency>

         <!--  spring framework -->

    <dependency>

          <groupId>org.springframework</groupId>

          <artifactId>spring-core</artifactId>

          <version>3.2.2.RELEASE</version>

    </dependency>

    <dependency>

          <groupId>org.springframework</groupId>

          <artifactId>spring-context</artifactId>

          <version>3.2.2.RELEASE</version>

    </dependency>

    <dependency>

          <groupId>org.springframework</groupId>

          <artifactId>spring-beans</artifactId>

          <version>3.2.2.RELEASE</version>

    </dependency>

         <!-- spring rabbit mq -->

                  <dependency>

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

                  <artifactId>spring-amqp</artifactId>

                  <version>1.2.0.RELEASE</version>

                  <exclusions>

                           <exclusion>

                                   <groupId>com.sun.jmx</groupId>

                                   <artifactId>jmxri</artifactId>

                           </exclusion>

                  </exclusions>

         </dependency>

         <dependency>

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

                  <artifactId>spring-rabbit</artifactId>

                  <version>1.2.0.RELEASE</version>

         </dependency>

         <dependency>

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

                  <artifactId>spring-erlang</artifactId>

                  <version>1.2.0.RELEASE</version>

         </dependency>

 

        

         <!--  JUnit 4 -->

         <dependency>

                  <groupId>junit</groupId>

                  <artifactId>junit</artifactId>

                  <version>4.10</version>

         </dependency>

                 

         <!--  logging framework -->

         <dependency>

           <groupId>org.slf4j</groupId>

           <artifactId>slf4j-api</artifactId>

           <version>1.7.5</version>

          </dependency>

          <dependency>

           <groupId>ch.qos.logback</groupId>

           <artifactId>logback-classic</artifactId>

           <version>1.0.13</version>

          </dependency>

          <dependency>

           <groupId>ch.qos.logback</groupId>

           <artifactId>logback-core</artifactId>

           <version>1.0.13</version>

          </dependency>

        

  </dependencies>

</project> 


applicationContext.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/rabbit

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

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 

    <!-- A reference to the org.springframework.amqp.rabbit.connection.ConnectionFactory -->

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

 

    <!-- Creates a org.springframework.amqp.rabbit.core.RabbitTemplate for access to the broker -->

    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>

 

    <!-- Creates a org.springframework.amqp.rabbit.core.RabbitAdmin  to manage exchanges, queues and bindings -->

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

 

    <!-- Creates a queue for consumers to retrieve messages -->

    <rabbit:queue name="simplequeue"/>

</beans>


Producer.java

 

package com.terry.rabbitmq.springframework;

 

import org.springframework.amqp.core.AmqpTemplate;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class Producer {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);

        amqpTemplate.convertAndSend("simplequeue", "Hello World");

    }

}


Consumer.java

package com.terry.rabbitmq.springframework;

 

import org.springframework.amqp.core.AmqpTemplate;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.context.support.GenericXmlApplicationContext;

 

public class Consumer {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);

        System.out.println(amqpTemplate.receive("simplequeue"));

        //System.exit(1);

    }

}

 

'클라우드 컴퓨팅 & NoSQL > RabbitMq' 카테고리의 다른 글

RabbitMQ 기본 기동  (1) 2014.01.02
RabbitMQ 공부 노트  (0) 2013.09.03
RabbitMQ multi threaded read message consumer  (1) 2013.08.27
RabbitMQ - Receive Message  (0) 2013.08.27
RabbitMQ - Send Message  (0) 2013.08.27