目前我们使用Springboot框架注解搭建生产者、消费者
/**
* 配置类定义别名
*
*/
@Configuration
public class MqConfig {
public static final String DRIVER_VEHICLE= "driver_vehicle";
/**
* 单个推送
* @return
*/
@Bean
public Queue drivPush() {
return new Queue(DRIVER_VEHICLE);
}
}
生产者:
import org.apache.log4j.Logger;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author Created by Yek on 2018/12/18.
* @create 2018-12-18-9:46
*/
@Component
public class MessageQueueUtils {
private Logger log = Logger.getLogger(this.getClass());
@Autowired
private AmqpTemplate rabbitTemplate;
/**
* @Author Created by aQing on 2019/03/20
* @method 保存信息到MQ
*/
public void saveDrvPoint(String data) {
log.info("新增数据到队列========>>>>>>"+MqConfig.DRIVER_VEHICLE);
rabbitTemplate.convertAndSend(MqConfig.DRIVER_VEHICLE, data);
}
}
消费者:
import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import static org.apache.commons.lang.StringUtils.isNotBlank;
/**
* @author Created by aQing on 2018/12/18.
* @create 2018-12-18-9:46
*/
@Component
public class SmsReceive {
private Logger log = Logger.getLogger(this.getClass());
@Autowired
private SmsLogService smsLogService;
/**
* @Author Created by Yek on 2018/12/18
* @method
*/
@RabbitListener(queues = "smsPush")
public void singlePush(String data) {
log.info("Push Receive:" + data);
if (!(null == data ? "" : data).isEmpty()) {
JSONObject msg = JSONObject.parseObject(data);
String msgUserId = msg.getString("msgUserId");
try {
boolean sms = smsLogService.autoSmsTime(msgUserId);
} catch (Exception e) {
log.error("Exception:" + e.getMessage());
}
log.info(“处理消息完成”)
}
}