Spring 整合active mq

Spring 整合active mq

public class QueueProducer {
   
	@Autowired
	private JmsTemplate jmsTemplate;
	//这里使用了Destination而没有使用ActiveMQQueue是因为后者是前者的实现类,在后面创建文本消息对象时使用的是Destination
	@Autowired
	private Destination queueTextDestination;
	@Component
	public void sendTextMessages(final String text) {
		jmsTemplate.send(queueTextDestination, new MessageCreator() {
			
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(text);
			}
		});
	}
}

@Component
public class TopicProducer {

@Autowired
private JmsTemplate jmsTemplate;
//这里使用了Destination而没有使用ActiveMQQueue是因为后者是前者的实现类,在后面创建文本消息对象时使用的是Destination
@Autowired
private Destination topicTextDestination;

public void sendTextMessages(final String text) {
	jmsTemplate.send(topicTextDestination, new MessageCreator() {
		
		public Message createMessage(Session session) throws JMSException {
			return session.createTextMessage(text);
		}
	});
}

}
public class MyMessageListener implements MessageListener {

public void onMessage(Message message) {
	//这个message就是最终的消息
	TextMessage textMessage = (TextMessage) message;
	try {
		System.out.println("最终的消息:"+textMessage.getText());
	} catch (JMSException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

}
项目应用
//itemSearchService.importList(list);
//导入到mq中,在导入前首先确定到如数据格式,1.字符串2.map 3.对象 4.二进制5.原生(类似于map)
//首先这个不是map(map里面也必须是可列化的才行),排除2,5,如果是对象必须实现序列化,但是list没有实现序列化,排除3,而流传输太麻烦(只有传图片时可以用到)
jmsTemplate.send(queueSolrDestination, new MessageCreator() {

				@Override
				public Message createMessage(Session session) throws JMSException {
					
					return session.createTextMessage(jsonString);
				}
			});

短信验证码

@Override
		public void createSmsCode(final String phone) {
			//1.生成验证码
			Random random = new Random();
			final String code = random.nextInt(999999)+100000+"";
			//2.将验证码放入缓存中,方便以后进行比较
			redisTemplate.boundHashOps("smsCode").put(phone, code);
			System.out.println("code : "+code);
		    //3.将验证码发送到消息中间件中发送验证码
			//明确这里是
			//这里是消息的生产者,发送消息使用spring 结合MQ,而接受方 使用的是springboot 结合MQ,这并不影响
			//因为这里只是存那边只用取
			//这里使用map和JSON转对象成json字符串避免了复杂的拼串
			jmsTemplate.send(queueDestination, new MessageCreator() {
				@Override
				public Message createMessage(Session session) throws JMSException {
					MapMessage message = session.createMapMessage();
					message.setString("mobile", phone);
					message.setString("signName", signName);
					message.setString("templateCode",templateCode);
					Map<String, String> map =new HashMap<String, String>();
					map.put("code", code);
					message.setString("param", JSON.toJSONString(map));
					return message;
				}
			});
		}

		@Override
		public boolean checkCode(String phone,String smsCode) {
			String code = (String) redisTemplate.boundHashOps("smsCode").get(phone);
			if (code == null ) {
				return false;
			}
			if (!code.equals(smsCode)) {
				return false;
			}
			return true;
		}

Springboot 消费方监听
@Component
public class SmsListener {

	@Autowired
	private SmsUtils smsUtils;
	//发送消息到mq,目的sms,采用map传参
	@JmsListener(destination="sms")
	public void sendSms(Map<String, String> map) {
		try {
			//实际上可以不用返回SendSmsResponse,这里只是为了查看结果
			SendSmsResponse response = smsUtils.sendSms(map.get("mobile"),map.get("signName") , map.get("templateCode"), map.get("param"));
			
			System.out.println("code:"+response.getCode());
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值