需求:为了实现手机app定时接收消息通知,采用定时任务的方式,每天两点将要发送的消息通知存放在消息队列中(延时的),到指定时间服务器接收队列,然后调用api发送到手机
定时任务ScheduleService
// 每天2点
@Scheduled(cron = "0 0 2 * * *")
fun handleMobileMessage(){
//其他代码略
val sendMap = HashMap<String, String>()
val diff = commUtil.getHourStart(8).time - Date().time //需要延时6个小时:2点到8点之间的时间差
sendMap["mq_type"] = "1" //延时类型
sendMap["msg_id"] = obj.id.toString() //通知id
mqSenderService.sendDelayMsg(sendMap,"1", diff) //调用队列方法
//其他代码略
}
消息队列.发送ActiveMqSenderService
@Throws(Exception::class)
fun sendDelayMsg(msgMap: Map<String, String>, type: String, delayTime: Long) {
val s = jmsMessagingTemplate.getConnectionFactory().createConnection().createSession(false, Session.AUTO_ACKNOWLEDGE)
var producer: MessageProducer? = null
var message: Message? = null
if (type == "1"){//客户拜访延时通知
message = s.createMapMessage()//这边用的是map消息格式
msgMap.keys.map { key ->
message.setString(key, msgMap.get(key))
}
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delayTime)//实现延时的主要代码
producer = s.createProducer(crmDelayQueue) //crmDelayQueue为队列名称
}
producer.send(message)
}
消息队列.接收ActiveMqRecieverService
@JmsListener(destination = "Dev_crm_delay_queue") //监听
fun crmDelayQueue(resp: Map<String, String>){
val mqType = resp["mq_type"]
when(mqType){
"1" -> custService.msgSend2Person(resp["msg_id"]!!.toLong())//调用发送手机的方法
else -> {}
}
}
注意这边是自产自销啦