很多编程语言框架里都提供了发布订阅的组件,或者叫事件处理机制,而spring框架对这个功能也有支持,主要使用EventListener
实现订阅,使用ApplicationEventPublisher
使用发布。这种系统集成的我们先叫它“集成组件”
事实上,发布订阅真的与开发语言没有什么关系,所以出现了另一种产品,消息中间件,或者叫消息队列,它是以发布订阅模式为理论基础的,同时很多消息队列产品又有自己的特色,这种独立的消息队列我们为rabbitmq
为例子。
@Async
注解基于以上的介绍,主要帮助大家理解和认识,在什么时候用什么类型的工具。
订阅
@Getter
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class CreateBookEvent {
private String address;
private String title;
}
@Component
public class EmailEventListener {
@EventListener
@Async
public void handleEvent(CreateBookEvent event) throws Exception {
System.out.println("email消息:建立图书:" + event.getTitle());
}
}
发布
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public void publish(){
applicationEventPublisher.publishEvent(CreateBookEvent.builder().address("system").title("新建图书").build());
}
订阅
@Slf4j
@Component
public class DistributorSubscriber {
public static final String WORK_QUEUE = "fx.activity.total";
public static final String EXCHANGE = "fx.exchange";
@Autowired
DistributorActivityTotalRepository distributorActivityTotalRepository;
@Autowired
ObjectMapper objectMapper;
@Bean
public TopicExchange phoneTotalExchange() {
return new TopicExchange(EXCHANGE);
}
@Bean
public Queue phoneTotalQueue() {
return new Queue(WORK_QUEUE);
}
@Bean
public Binding bindSignQueue() {
return BindingBuilder.bind(phoneTotalQueue()).to(phoneTotalExchange()).with(WORK_QUEUE);
}
@RabbitListener(queues = WORK_QUEUE)
public void phoneTotalQueueListener(String data) {
try {
logger.debug("fx.activity.total:{}", data);
DistributorActivityTotal entity =
objectMapper.readValue(data, DistributorActivityTotal.class);
distributorActivityTotalRepository.incUpdate(entity);
} catch (Exception ex) {
logger.error("fx.activity.total.error", ex);
}
}
发布
@Autowired
private RabbitTemplate rabbitTemplate;
public void modifySalesperson(SalesPersonDTO salesPersonDTO) {
try {
rabbitTemplate.convertAndSend(
"EXCHANGE",
"MQName",
objectMapper.writeValueAsString(salesPersonDTO)
);
logger.debug("Enter {},message:{}", "modifySalesperson", salesPersonDTO.toString());
} catch (Exception ex) {
logger.error("MQ.modifySalesperson.error", ex);
}
}
原文:https://www.cnblogs.com/lori/p/10824990.html