RabbitTemplate是我们在与SpringAMQP整合的时候进行发送消息的关键类
该类提供了丰富的发送消息的方法,包括可靠性消息投递、回调监听消息接口ConfirmCallback、返回值确认接口
ReturnCallback等等同样我们需要注入到Spring容器中,然后直接使用。
在与spring整合时需要实例化,但是在与Springboot整合时,只需要添加配置文件即可

首先将其注入到bean里面:

    @Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
return rabbitTemplate;
}

然后在其他类引用:

package com.dwz.spring;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
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 TestDwz {
@Autowired
private RabbitTemplate rabbitTemplate; @Test
public void testMessage01() {
MessageProperties messageProperties = new MessageProperties();
messageProperties.getHeaders().put("desc", "信息描述。。。");
messageProperties.getHeaders().put("type", "自定义消息类型。。。");
Message message = new Message("Hello RabbitMQ!".getBytes(), messageProperties); rabbitTemplate.convertAndSend("topic001", "spring.amqp", message, new MessagePostProcessor() { @Override
public Message postProcessMessage(Message message) throws AmqpException {
System.out.println("----添加额外的设置----");
message.getMessageProperties().getHeaders().put("desc", "额外修改的信息描述");
message.getMessageProperties().getHeaders().put("attr", "额外新加的属性");
return message;
}
});
} @Test
public void testMessage02() {
//创建消息
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType("text/plain");
Message message = new Message("spring 消息".getBytes(), messageProperties);
rabbitTemplate.send("topic001", "spring.abc", message); rabbitTemplate.convertAndSend("topic001", "spring.amqp", "hello object message send!");
}
}

消息模板-RabbitTemplate的更多相关文章

  1. .NET之微信消息模板推送

    最近在项目中使用到了微信消息模板推送的功能,也就是将对应的消息推送到对应的用户微信上去,前提是你必须要有一个微信公众号并且是付费了的才会有这个功能,还有就是要推送的用户必须是的关注了你的微信公众号的. ...

  2. 微信公众号发送消息模板(java)

    这段时间接触公众号开发,写下向用户发送消息模板的接口调用 先上接口代码 public static JSONObject sendModelMessage(ServletContext context ...

  3. 原创:【微信小程序】发送消息模板教程(后台以PHP示例)

    1.本教程对外开放,未经博主同意,禁止转载. 2.准备材料:1)公众号|小程序,添加选择的模板消息,2)在设置>开发设置页面,开通消息模板功能:如: 3.因为调用微信发送模板的接口是:https ...

  4. Spring集成RabbitMQ-连接和消息模板

    ConnectionFactory ConnectionFactory是RabbitMQ服务掌握连接Connection生杀大权的重要组件 有了它,就可以创建Connection(org.spring ...

  5. Java使用HTTPClient3.0.1开发的公众平台消息模板的推送功能

    package com.company.product.manager.busniess.impl; import java.io.IOException;import java.nio.charse ...

  6. 微信小程序消息模板

    wxml: <form bindsubmit='sendSms' report-submit='true' id='fo'> <button form-type='submit'&g ...

  7. appid、appkey、appsecret、accesstoken,消息模板

    app_id, app_key, app_secret , 对于平台来说, 需要给你的 你的开发者账号分配对应的权限:1. app_id 是用来标记你的开发者账号的, 是你的用户id, 这个id 在数 ...

  8. .net推送微信消息模板

    1.获取access_token public string GetAccess_Token() { string appid = WxPayConfig.APPID; string appsecre ...

  9. 钉钉机器人集成Jenkins推送消息模板自定义发送报告

    一.由于公司同样也使用了钉钉.那么在做Jenkins集成自动化部署的时候,也是可以集成钉钉的. 那种Jenkins下载钉钉插件集成,简单设置就可以完成了.我们今天要做的是,定制化的发送消息. 钉钉推送 ...

随机推荐

  1. Windows authentication for WCF web services error

    WCF Web服务的Windows身份验证在部署到IIS时,默认网站添加应用程序的方式.浏览运行.svc文件报错. 错误代码: The authentication schemes configure ...

  2. centos7上搭建NFS的实践

    NFS 即network file system 可用于向k8s集群提供持久存储 最小化安装centos后  把网卡设置好了后 1.关闭防火墙 [root@NFS ~]# systemctl stop ...

  3. lua加载DLL

    .cpp //若没有在项目属性--库文件.依赖文件.包含添加.则添加一下路径 #pragma  comment (lib,"lua5.1.lib") #include " ...

  4. lua与c的交互(运用)

    (1)lua程序 (2)C++程序(头文件) extern "C" {     #include "lua.h"     #include "lual ...

  5. 如何使用Navicat 创建一个SqlServer定时任务

    因为网上资料不全,所以自己琢磨了一上午,终于弄出来了,记录一下. step1: 右击[函数]选择[新建函数]添加一个存储过程 step2: 选择[过程],点击下一步直至完成,然后编辑存储过程,保存 s ...

  6. Leaflet个人封装笔记

    <!DOCTYPE html> <html> <head> <link href="style/leaflet.css" type=&qu ...

  7. python 常见内置函数setattr、getattr、delattr、setitem、getitem、delitem

    常见内置函数 内置函数:在类的内部,特定时机自动触发的函数 示例1:setattr.getattr.delattr class Person: # def __init__(self, name): ...

  8. Windows命令行命令总结

    转载地址:https://www.cnblogs.com/accumulater/p/7110811.html   1. gpedit.msc-----组策略 2. sndrec32-------录音 ...

  9. Spring自定义标签的实现

    首先 简单写下 spring xml解析的过程 通过一段简单的 调用spring代码开始 public static void main(String[] args) { ApplicationCon ...

  10. php--常见算法2

    <?php function zhi($number){ $f1=1; $f2=1; for($i=3;$i<=$number;$i++){ //前一个的前一个值+前一个值 $f3=$f1 ...