简单使用
public class StateListener implements ApplicationListener<ContextRefreshedEvent> {
@Resource
private ConnectionFactory connectionFactory;
@Bean
public Queue serviceQueue() {
return new Queue(SERVICE_QUEUE);
}
/**
* 创建服务自己的队列
*/
private static final String SERVICE_QUEUE = QueueConstants.SysBpmQueue.SYS_BPM_FORM_PROCESS_STATE_QUEUE + StringPool.DASH + FormConstants.APPLICATION_NAME;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (connectionFactory != null) {
Connection connection = connectionFactory.createConnection();
Channel channel = connection.createChannel(false);
try {
//实例化一个 持久化 非独占 空闲不删除 无其余参数的队列
channel.queueDeclare(SERVICE_QUEUE, true, false, false, null);
//绑定队列和交换机,第三个参数未routingKey
channel.queueBind(SERVICE_QUEUE, QueueConstants.SysBpmQueue.SYS_BPM_FORM_PROCESS_STATE_EXCHANGE, StringPool.EMPTY);
} catch (Exception e) {
log.error(e.getMessage());
}
}
}
@RabbitListener(queues = SERVICE_QUEUE)
private void listener(byte[] bytes) {
try {
// 业务实现
} catch (Exception e) {
log.error("流程状态回写失败:", e);
}
}
}
queueDeclare()的解释如下:
/**
*
* @param queue 队列名称
* @param durable 服务器重启时能够存活
* @param exclusive 连接断开后会删除队列
* @param autoDelete 当没有消费者删除队列
* @param arguments 参数设置
*/
queueDeclare(String queue,Boolean durable,Boolean exclusive,Boolean autoDelete,String arguments)
yml中配置host、port、账号密码信息
posted on 2023-12-13 17:35 nitianxiaozi 阅读(36) 评论(0) 收藏 举报
浙公网安备 33010602011771号