Last active
February 7, 2019 14:26
-
-
Save crazydevman/e0d481355020a6dbd396c036a870d734 to your computer and use it in GitHub Desktop.
Sample code to publish and listen for events with conditions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.5.19.RELEASE</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> | |
<groupId>com.example</groupId> | |
<artifactId>demo</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>demo</name> | |
<description>Demo project for Spring Boot</description> | |
<properties> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-devtools</artifactId> | |
<scope>runtime</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
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; | |
@SpringBootApplication | |
public class DemoApplication implements CommandLineRunner { | |
@Autowired | |
private ApplicationEventPublisher publisher; | |
@Override | |
public void run(String... args) throws Exception { | |
OrderCreatedEvent event = new OrderCreatedEvent(true); | |
publisher.publishEvent(event); | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(DemoApplication.class, args); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.context.event.EventListener; | |
import org.springframework.stereotype.Component; | |
@Component | |
public class MyComponent { | |
private static final Logger LOGGER = LoggerFactory.getLogger(MyComponent.class); | |
@EventListener(condition = "#createdEvent.awesome") | |
public void handleOrderCreatedEvent(OrderCreatedEvent createdEvent) { | |
LOGGER.info("Awesome event handled"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
public class OrderCreatedEvent { | |
public OrderCreatedEvent(boolean awesome) { | |
this.awesome = awesome; | |
} | |
private boolean awesome; | |
public boolean isAwesome() { | |
return awesome; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.boot.test.mock.mockito.MockBean; | |
import org.springframework.context.ApplicationEventPublisher; | |
import org.springframework.test.context.junit4.SpringRunner; | |
import static org.mockito.Mockito.*; | |
@RunWith(SpringRunner.class) | |
@SpringBootTest(classes = MyComponent.class) | |
public class DemoApplicationTests { | |
@Autowired | |
private ApplicationEventPublisher publisher; | |
@MockBean | |
private MyComponent myComponent; | |
@Test | |
public void handleOrderCreatedEvent_shouldExecute_whenAwesome() { | |
OrderCreatedEvent event = new OrderCreatedEvent(true); | |
publisher.publishEvent(event); | |
verify(myComponent).handleOrderCreatedEvent(event); | |
} | |
@Test | |
public void handleOrderCreatedEvent_shouldNotExecute_whenNotAwesome() { | |
OrderCreatedEvent event = new OrderCreatedEvent(false); | |
publisher.publishEvent(event); | |
verify(myComponent, times(0)).handleOrderCreatedEvent(event); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment