rabbitMQ安装和例子

记录自用

一 安装

A win系统:

1 下载安装 http://www.erlang.org/downloads Erlang语言环境

2 配置环境变量 ERLANG_HOME=D:\Program Files\erl9.3

   以及Path,在原来的值后面加上“;%ERLANG_HOME%\bin”

3 下载安装 http://www.rabbitmq.com/install-windows-manual.html 

4 配置环境变量RABBITMQ_HOEM=D:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.5

    以及Path,在原来的值后面加上“;%RABBITMQ_HOME%\sbin”

5 启用web管理,在sbin下cmd执行 rabbitmq-plugins enable rabbitmq_management,可在浏览器访问http://localhost:15672,账户guest/guest

 

二 例子

A 手写

maven

<dependencies>
    <dependency>
        <groupId>com.rabbitmq</groupId>
        <artifactId>amqp-client</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>

生产者

package com;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.security.Provider;
import java.util.concurrent.TimeoutException;

public class Test1 {
    static String qName="queue1";

    public static void main(String[] args) {
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("localhost");
        Connection c = null;
        Channel ch = null;
        try {
            c = f.newConnection();
            ch = c.createChannel();
            //创建队列 (队列声明) 参数:队列名,是否持久化,是否通道独占,不使用时自动删除,其他参数
            ch.queueDeclare(qName,false,false,false,null);
            String msg[] = {"a1","b","c1","d","e"};
            for(int i=0;i<msg.length;i++){
                /*try {
                    new Thread().sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
                //插入队列 参数:路由器,路由键,基本属性,消息体
                ch.basicPublish("",qName,null,msg[i].getBytes());
                System.out.println("in "+msg[i]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }finally {
            if(ch!=null){
                try {
                    ch.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
            if(c!=null){
                try {
                    c.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

消费者

package com;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Test2 {
    public static void main(String[] args) {
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("localhost");
        Connection c;
        Channel ch;

        try {
            c = f.newConnection();
            ch = c.createChannel();
            //队列声明,与生产者相同
            ch.queueDeclare(Test1.qName,false,false,false,null);
            Consumer co = new DefaultConsumer(ch){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    String msg = new String(body,"UTF-8");
                    System.out.println("out "+msg);
                }
            };
            //消费 参数:队列名,自动确认,接受消息者
            ch.basicConsume(Test1.qName,true,co);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        //不关闭连接,可持续消费队列中出现的对象
    }
}

B spring

pom

<?xml version="1.0" encoding="UTF-8"?>
<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>t1</groupId>
    <artifactId>t1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- 引入amqp依赖,它能很好的支持RabbitMQ -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <!-- 引入test依赖,这次需要用到JUnit -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

</project>

application.properties

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
server.port=8081
server.address=127.0.0.1

Spring启动类Application.java

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@ServletComponentScan
@EnableScheduling
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

控制层(自便)Controller

package com.controller;

import com.component.Sender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/")
public class IndexContro {

    @Autowired
    private Sender s;

    //通过http://localhost:8081/index手动访问激活
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(){
        s.send();
        System.out.println("controller");
        return "";
    }
}

组件,生产者Sender

package com.component;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//生产
@Component
public class Sender {
    @Autowired
    AmqpTemplate at;

    //发送
    public void send(){
        String content = "qwer";
        //队列名,内容
        at.convertAndSend("hh",content);
        System.out.println("Sender "+content);
    }
}

组件,消费者Receiver

package com.component;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//消费
@Component
@RabbitListener(queues = "hh")//队列名
public class Receiver {

    //消费
    @RabbitHandler
    public void receiver(String str){
        System.out.println("receiver "+str);
    }
}

队列配置Config

package com.config;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    @Bean
    public Queue helloQueue(){
        return new Queue("hh");
    }

}

目录结构

启动后访问 http://127.0.0.1:8081/index ,可在控制台查看效果

 

参考:

1 安装 https://www.cnblogs.com/sam-uncle/p/9050242.html

2 例子 https://www.cnblogs.com/sam-uncle/p/9188426.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值