这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.springevents.asynchronous;

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.scheduling.annotation.Async;
import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW;
import com.baeldung.springevents.synchronous.CustomSpringEvent;

@Component
public class AnnotationDrivenEventListener {

@EventListener
@Async
public void handleAsyncEvent(CustomSpringEvent event) {
System.out.println("Handle event asynchronously: " + event.getMessage());
}

@Async
@Transactional(propagation = REQUIRES_NEW)
@TransactionalEventListener
void handleCustom(CustomSpringEvent event) {
/* … */
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableAsync
@Configuration
@ComponentScan("com.baeldung.springevents.synchronous")
public class AsynchronousSpringEventsConfig {
Expand Down