当前位置: 移动技术网 > IT编程>开发语言>Java > rabbitmq学习笔记2——消息发送与接收

rabbitmq学习笔记2——消息发送与接收

2018年11月11日  | 移动技术网IT编程  | 我要评论

创建项目

使用idea(版本2018.2),基于jdk1.8,springboot版本2.1,maven3.5.4

分别创建3个项目,group都为com.study.rabbitmq

项目1:framework,框架,存放共用的实体类

项目2:producer,生产者,存放消息发送服务

项目3:consumer,消费者,存放消息处理服务

1.framework项目

创建package,命名为entity,在其下创建实体类order,只为模拟,设置了两个属性,id与no

注意:需要实现serializable接口,否则消息进行序列化时会报错。

package com.study.rabbitmq.framework.entity;

import lombok.data;

import java.io.serializable;

@data
public class order implements serializable {
    private string id;
    private string no;

}

2.producer项目

a.pom中增加对framework的依赖

<dependency>
<groupid>com.study.rabbitmq</groupid>
<artifactid>framework</artifactid>
<version>0.0.1-snapshot</version>
</dependency>

b.配置文件中增加rabbitmq连接信息

spring:
rabbitmq:
addresses: 127.0.0.1
port: 5672
username: guest
password: guest
virtual-host: /

server:
port: 8080

c.创建package,命名为service,其下创建消息发送服务,命名为messagesendservice

package com.study.rabbitmq.producer.service;


import lombok.extern.slf4j.slf4j;
import org.springframework.amqp.rabbit.connection.correlationdata;
import org.springframework.amqp.rabbit.core.rabbittemplate;
import org.springframework.beans.factory.annotation.autowired;
import com.study.rabbitmq.framework.entity.order;
import org.springframework.stereotype.service;

import java.util.uuid;

@service
@slf4j
public class messagesendservice {
    @autowired
    private rabbittemplate rabbittemplate;

    public void sendorder(order order)
    {
        string messageid=uuid.randomuuid().tostring();
        log.info(messageid);
        correlationdata correlationdata=new correlationdata();
        correlationdata.setid(messageid);
        rabbittemplate.convertandsend("order-exchange","123",order,correlationdata);
    }


}

3.consumer项目

a.pom中增加对framework的依赖

<dependency>
<groupid>com.study.rabbitmq</groupid>
<artifactid>framework</artifactid>
<version>0.0.1-snapshot</version>
</dependency>

b.配置文件中增加rabbitmq连接信息

spring:
rabbitmq:
addresses: 127.0.0.1
port: 5672
username: guest
password: guest

listener:
simple:
acknowledge-mode: manual
concurrency: 5
max-concurrency: 10


server:
port: 8081


 c.创建package,命名为service,其下创建消息处理服务,命名为messagehandleservice

 

package com.study.rabbitmq.consumer.service;

import com.study.rabbitmq.framework.entity.order;
import lombok.extern.slf4j.slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.messaging.handler.annotation.payload;
import org.springframework.stereotype.service;

@service
@slf4j
public class messagehanlerservice {



    @rabbitlistener(bindings = @queuebinding(
            value = @queue("order-queue"),
            exchange =@exchange("order-exchange"),
            key ="123"
    ))
    @rabbithandler
    public void handleorder(@payload order order)
    {
        log.info("开始处理订单信息");
        log.info("订单编号为:"+order.getno());

    }
}

 

配置rabbitmq服务


使用rabbitmq自带的管理页面,http://localhost:15672/#/

1.创建exchange,命名为order-exchange

2.创建queue,命名为order-queue

3.绑定二者,路由key设置为123

我们的应用向rabbitmq发送消息需要以上信息。

 此处涉及一些rabbitmq的基本概念,交换机、队列,请自行了解rabbitmq框架体系。

 

发送消息测试

为producer项目下的messagesendservice创建junit测试

 

package com.study.rabbitmq.producer.service;


import com.study.rabbitmq.framework.entity.order;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;

import org.springframework.test.context.junit4.springrunner;


@runwith(springrunner.class)
@springboottest
public class messagesendservicetest {

    @autowired
    private messagesendservice orderservice;

    @test
    public void sendorder() {
        order order=new order();
        order.setid("1");
        order.setno("单号1247");

        orderservice.sendorder(order);

    }
}

 

运行测试


无报错,进入rabbitmq管理页面,http://www.lhsxpumps.com/_localhost:15672/#/,进入queues的tab页,可看到,已经收到一条消息处于ready状态

 接收消息

启动运行consumer项目,查看控制台输出的消息,确实消息已被接收及处理。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网