2018年3月9日 | Leave a comment 解决: 不用 MQ 在 JVM spring 内进行event 发布订阅 App.java package demo.spevent; import java.util.logging.Level; import java.util.logging.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Bean; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; @SpringBootApplication public class App { private static final Logger LOG = Logger.getLogger(App.class.getName()); public static void main(String[] argv) { SpringApplication.run(App.class, argv); } @Bean public CommandLineRunner cmdRunner(ItemService s) { return (argv) -> { s.updateItem(new Item(2)); }; } @Component public static class L { @EventListener public void onChange(ItemStatusChangeEvent e) { LOG.log(Level.INFO, "onChange e:{0}", e); } } @Component public static class L2 { @EventListener public void onChange2(ItemStatusChangeEvent e) { LOG.log(Level.INFO, "onChange2 e:{0}", e); } } @Service public static class ItemService { private static final Logger LOG = Logger.getLogger(ItemService.class.getName()); private final ApplicationEventPublisher eventPub; @Autowired public ItemService( ApplicationEventPublisher eventPub) { this.eventPub = eventPub; } public void updateItem(Item i) { LOG.info("updateItem ...."); eventPub.publishEvent(new ItemStatusChangeEvent(i)); } } public static class Item { public Item(int status) { this.status = status; } int status; public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } @Override public String toString() { return "Item{" + "status=" + status + '}'; } } public static class ItemStatusChangeEvent { private final Item item; public ItemStatusChangeEvent(Item i) { this.item = i; } @Override public String toString() { return "ItemStatusChangeEvent{" + "item=" + item + '}'; } } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 package demo.spevent; import java.util.logging.Level;import java.util.logging.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationEventPublisher;import org.springframework.context.annotation.Bean;import org.springframework.context.event.EventListener;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service; @SpringBootApplicationpublic class App { private static final Logger LOG = Logger.getLogger(App.class.getName()); public static void main(String[] argv) { SpringApplication.run(App.class, argv); } @Bean public CommandLineRunner cmdRunner(ItemService s) { return (argv) -> { s.updateItem(new Item(2)); }; } @Component public static class L { @EventListener public void onChange(ItemStatusChangeEvent e) { LOG.log(Level.INFO, "onChange e:{0}", e); } } @Component public static class L2 { @EventListener public void onChange2(ItemStatusChangeEvent e) { LOG.log(Level.INFO, "onChange2 e:{0}", e); } } @Service public static class ItemService { private static final Logger LOG = Logger.getLogger(ItemService.class.getName()); private final ApplicationEventPublisher eventPub; @Autowired public ItemService( ApplicationEventPublisher eventPub) { this.eventPub = eventPub; } public void updateItem(Item i) { LOG.info("updateItem ...."); eventPub.publishEvent(new ItemStatusChangeEvent(i)); } } public static class Item { public Item(int status) { this.status = status; } int status; public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } @Override public String toString() { return "Item{" + "status=" + status + '}'; } } public static class ItemStatusChangeEvent { private final Item item; public ItemStatusChangeEvent(Item i) { this.item = i; } @Override public String toString() { return "ItemStatusChangeEvent{" + "item=" + item + '}'; } } } pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>demo</groupId> <artifactId>spevent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> </project> 123456789101112131415161718192021222324 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>demo</groupId> <artifactId>spevent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties></project> 参考: http://www.sohu.com/a/195441807_355142