package com.roy.springboot.producer;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import com.roy.springboot.util.MyConstants;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@RestController
public class ProducerController {
@Autowired
private RabbitTemplate rabbitTemplate;
// @Autowired
// private AmqpTemplate amqpTemplate;
@ApiOperation(value="direct发送接口",notes="直接发送到队列。task模式")
@GetMapping(value="/directSend")
public Object directSend(String message) throws AmqpException, UnsupportedEncodingException {
//设置部分请求参数
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
messageProperties.setPriority(2);
//设置消息转换器,如json
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//将对象转换成json再发送。
// rabbitTemplate.convertandsend("",Object);
//发消息
rabbitTemplate.send("directqueue",new Message(message.getBytes("UTF-8"),messageProperties));
return "message sended : "+message;
}
@ApiOperation(value="fanout发送接口",notes="发送到fanoutExchange。消息将往该exchange下的所有queue转发")
@GetMapping(value="/fanoutSend")
public Object fanoutSend(String message) throws AmqpException, UnsupportedEncodingException {
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
//fanout模式只往exchange里发送消息。分发到exchange下的所有queue
rabbitTemplate.send(MyConstants.EXCHANGE_FANOUT, "", new Message(message.getBytes("UTF-8"),messageProperties));
Message message2 = MessageBuilder.withBody(message.getBytes()).setMessageId(UUID.randomUUID().toString()).build();
rabbitTemplate.send(message2);
return "message sended : "+message;
}
@ApiOperation(value="topic发送接口",notes="发送到topicExchange。exchange转发消息时,会往routingKey匹配的queue发送,*代表一个单词,#代表0个或多个单词。")
@ApiImplicitParam(name="routingKey",value="路由关键字")
@GetMapping(value="/topicSendHunanIT")
public Object topicSend(String routingKey,String message) throws AmqpException, UnsupportedEncodingException {
if(null == routingKey) {
routingKey="hebei.IT";
}
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
//fanout模式只往exchange里发送消息。分发到exchange下的所有queue
rabbitTemplate.send("topicExchange", routingKey, new Message(message.getBytes("UTF-8"),messageProperties));
return "message sended : routingKey >"+routingKey+";message > "+message;
}
@ApiOperation(value="header发送接口",notes="发送到headerExchange。exchange转发消息时,不再管routingKey,而是根据header条件进行转发。")
@GetMapping(value="/headerSend")
public Object headerSend(String txTyp,String busTyp,String message) throws AmqpException, UnsupportedEncodingException {
if(null == txTyp) {
txTyp="0";
}
if(null == busTyp) {
busTyp="0";
}
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
messageProperties.setHeader("txTyp", txTyp);
messageProperties.setHeader("busTyp", busTyp);
//fanout模式只往exchange里发送消息。分发到exchange下的所有queue
rabbitTemplate.send("headerExchange", "uselessRoutingKey", new Message(message.getBytes("UTF-8"),messageProperties));
return "message sended : txTyp >"+txTyp+";busTyp > "+busTyp;
}
@ApiOperation(value="quorum队列发送接口",notes="直接发送到队列。Quorum队列")
@GetMapping(value="/directQuorum")
public Object directQuorum(String message) throws AmqpException, UnsupportedEncodingException {
//设置部分请求参数
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
messageProperties.setPriority(2);
//设置消息转换器,如json
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//将对象转换成json再发送。
// rabbitTemplate.convertandsend("",Object);
//发消息
rabbitTemplate.send(MyConstants.QUEUE_QUORUM,new Message(message.getBytes("UTF-8"),messageProperties));
return "message sended : "+message;
}
// @ApiOperation(value="stream队列发送接口",notes="直接发送到队列。stream队列")
// @GetMapping(value="/directStream")
public Object directStream(String message) throws AmqpException, UnsupportedEncodingException {
//设置部分请求参数
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
messageProperties.setPriority(2);
//设置消息转换器,如json
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//将对象转换成json再发送。
// rabbitTemplate.convertandsend("",Object);
//发消息
rabbitTemplate.send(MyConstants.QUEUE_STREAM,new Message(message.getBytes("UTF-8"),messageProperties));
return "message sended : "+message;
}
}
Promise_J_Z
- 粉丝: 82
- 资源: 3
最新资源
- ouc2024秋攻防先导作业
- ouc2024秋攻防先导作业
- 算数优化算法AOA对BP的权值和阈值做优化,建立多个特征变量输入,单个因变量输出的拟合预测建模 程序内注释详细,可学习性强 直接替数据就可以用,可以直接导入excel数据 程序语言为matlab
- 精确符合规范反应谱的人工合成地震波
- LINUX最新版本算法库5.0.15(1).zip
- Matlab基于量子遗传算法的函数寻优方法 量子遗传算法QGA是量子计算与遗传算法相结合的产物,是一种新发展起来的概率进化算法 代码可正常运行
- 驾校预约学习系统 源码+数据库+论文(JAVA+SpringBoot+Vue.JS+MySQL).zip
- 基于ssm+mysql图书仓储管理系统源码数据库.docx
- springboot3+vue3
- CREO7装配与产品设计-钟日铭-配套操作文件
- 基于SpringBoot+Vue.JS开发的校园志愿者管理系统 JAVA毕业设计 源码+数据库+论文(有项目截图)+启动教程
- Linux操作系统课程设计实验报告-涵盖多线程同步、内核模块编程、进程树打印、系统调用添加及块设备驱动实现
- 素材网站解析源码支持21个网站解析可搭建运营
- 285个地级市灯光数据和雾霾数据.zip
- 全桥谐振和变压器计算 看截图,知识文档,含有LLC谐振半桥计算,全桥LLC谐振和变压器的计算,描述详细使用,表格都有引用公式依据,便于理解
- vlan dnsp 111
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈