4.2 RabbitMQ配置
向RabbitMQ声明两个队列:添加选课、完成选课,交换机使用路由模式,代码如下:
[AppleScript] 纯文本查看 复制代码 @Configuration public class RabbitMQConfig {
//添加选课任务交换机
public static final String EX_LEARNING_ADDCHOOSECOURSE = "ex_learning_addchoosecourse";
//添加选课消息队列
public static final String XC_LEARNING_ADDCHOOSECOURSE = "xc_learning_addchoosecourse";
//完成添加选课消息队列
public static final String XC_LEARNING_FINISHADDCHOOSECOURSE = "xc_learning_finishaddchoosecourse";
//添加选课路由key
public static final String XC_LEARNING_ADDCHOOSECOURSE_KEY = "addchoosecourse";
//完成添加选课路由key
public static final String XC_LEARNING_FINISHADDCHOOSECOURSE_KEY = "finishaddchoosecourse";
/** * 交换机配置
* @return the exchange
*/
@Bean(EX_LEARNING_ADDCHOOSECOURSE)
public Exchange EX_DECLARE() {
return ExchangeBuilder.directExchange(EX_LEARNING_ADDCHOOSECOURSE).durable(true).build();
} //声明队列
@Bean(XC_LEARNING_FINISHADDCHOOSECOURSE)
public Queue QUEUE_XC_LEARNING_FINISHADDCHOOSECOURSE() {
Queue queue = new Queue(XC_LEARNING_FINISHADDCHOOSECOURSE);
return queue;
}
//声明队列
@Bean(XC_LEARNING_ADDCHOOSECOURSE)
public Queue QUEUE_XC_LEARNING_ADDCHOOSECOURSE() {
Queue queue = new Queue(XC_LEARNING_ADDCHOOSECOURSE);
return queue;
}
/**
* 绑定队列到交换机 .
* @param queue
the queue
* @param exchange the exchange
* @return the binding
*/
@Bean
public Binding BINDING_QUEUE_FINISHADDCHOOSECOURSE(@Qualifier(XC_LEARNING_FINISHADDCHOOSECOURSE) Queue queue, @Qualifier(EX_LEARNING_ADDCHOOSECOURSE) Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(XC_LEARNING_FINISHADDCHOOSECOURSE_KEY).noargs();
}
@Bean
public Binding BINDING_QUEUE_ADDCHOOSECOURSE(@Qualifier(XC_LEARNING_ADDCHOOSECOURSE) Queue queue, @Qualifier(EX_LEARNING_ADDCHOOSECOURSE) Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(XC_LEARNING_ADDCHOOSECOURSE_KEY).noargs();
} }
4.3 查询前N条任务
4.3.1 Dao
在XcTaskRepository中自定义方法如下:
[AppleScript] 纯文本查看 复制代码 public interface XcTaskRepository extends JpaRepository<XcTask, String> {
//取出指定时间之前的记录
Page<XcTask> findByUpdateTimeBefore(Pageable pageable,Date updateTime);
}
4.3.2 Service
[AppleScript] 纯文本查看 复制代码 @Service public class TaskService {
@Autowired
XcTaskRepository xcTaskRepository;
@Autowired RabbitTemplate rabbitTemplate;
//取出前n条任务,取出指定时间之前处理的任务
public List<XcTask> findTaskList(Date updateTime,int n){
//设置分页参数,取出前n 条记录
Pageable pageable = new PageRequest(0, n);
Page<XcTask> xcTasks = xcTaskRepository.findByUpdateTimeBefore(pageable,updateTime);
return xcTasks.getContent();
}
}
4.5.3 编写任务类
编写任务类,每分钟执行任务,启动订单工程,观察定时发送消息日志,观察rabbitMQ队列中是否有消息,代码 如下:
[AppleScript] 纯文本查看 复制代码 package com.xuecheng.order.mq;
@Component public class ChooseCourseTask {
private static final Logger LOGGER = LoggerFactory.getLogger(ChooseCourseTask.class);
@Autowired
TaskService taskService;
//每隔1分钟扫描消息表,向mq发送消息
@Scheduled(fixedDelay = 60000)
public void sendChoosecourseTask(){
//取出当前时间1分钟之前的时间
Calendar calendar =new GregorianCalendar();
calendar.setTime(new Date());
calendar.add(GregorianCalendar.MINUTE,‐1);
Date time = calendar.getTime();
List<XcTask> taskList = taskService.findTaskList(time, 1000);
} }
启动工程,测试读取任务列表的功能。
|