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();
}
}
}