Skip to content

Instantly share code, notes, and snippets.

@crazydevman
Last active February 7, 2019 14:26
Show Gist options
  • Save crazydevman/e0d481355020a6dbd396c036a870d734 to your computer and use it in GitHub Desktop.
Save crazydevman/e0d481355020a6dbd396c036a870d734 to your computer and use it in GitHub Desktop.
Sample code to publish and listen for events with conditions
<?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>
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);
}
}
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");
}
}
package com.example.demo;
public class OrderCreatedEvent {
public OrderCreatedEvent(boolean awesome) {
this.awesome = awesome;
}
private boolean awesome;
public boolean isAwesome() {
return awesome;
}
}
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