@Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { if (methodFound) { return; } TransactionalEventListener listener = AnnotatedElementUtils.findMergedAnnotation(method, TransactionalEventListener.class); if (listener == null) { return; } this.methodFound = true; bean = createCompletionRegisteringProxy(bean); }
@TransactionalEventListener public void saveClaim(ClaimIssuedEvent event) { try { coreCaseDataService.save(event.getAuthorisation(), event.getClaim()); } catch (CoreCaseDataStoreException ex) { logger.error(ex.getMessage(), ex); } }
private static boolean isTransactionalApplicationEventListener(ApplicationListener<?> listener) { Class<?> targetClass = AopUtils.getTargetClass(listener); if (!ApplicationListenerMethodAdapter.class.isAssignableFrom(targetClass)) { return false; } Field field = ReflectionUtils.findField(ApplicationListenerMethodAdapter.class, "method"); ReflectionUtils.makeAccessible(field); Method method = (Method) ReflectionUtils.getField(field, listener); return AnnotatedElementUtils.hasAnnotation(method, TransactionalEventListener.class); }
@Async @TransactionalEventListener void on(OrderCompleted event) { log.info("Sending email for order {}.", event.getOrder()); try { Thread.sleep(1000); } catch (InterruptedException o_O) {} log.info("Successfully sent email for order {}.", event.getOrder()); }
@TransactionalEventListener public void after(final AnyDeletedEvent event) { LOG.debug("About to delete index for {}[{}]", event.getAnyTypeKind(), event.getAnyKey()); DeleteResponse response = client.prepareDelete( AuthContextUtils.getDomain().toLowerCase(), event.getAnyTypeKind().name(), event.getAnyKey()). get(); LOG.debug("Index successfully deleted for {}[{}]: {}", event.getAnyTypeKind(), event.getAnyKey(), response); }
@TransactionalEventListener public void handle(PayloadApplicationEvent<TestModel> event) { System.err.println(event.getPayload().getName()); //这里可以记录日志, 发送消息等操作. //这里抛出异常, 会导致addTestModel方法异常, 但不会回滚事务. //注意, ApplicationEventPublisher不能使用线程池, 否则不会执行到这里 //因为, 包装类是通过ThreadLocal来判断当前是否有活动的事务信息. //TransactionalEventListener.fallbackExecution就是为了决定当当前线程没有事务上下文时, //是否还调用 handle 方法, 默认不调用. }
@TransactionalEventListener public void on(DomainEvent event) {}
@TransactionalEventListener public void on(DomainEvent event) { throw new IllegalStateException(); }
@Async @TransactionalEventListener public UserEmailChangedEvent handleUserEmailChanged(UserEmailChangedEvent emailChangedEvent) { System.out.println("UserEmailChangedEvent:" + emailChangedEvent.getSource()); return null; }
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT) public void doSomething(TransactionNotifyEvent transactionNotifyEvent) { System.out.println("提交之前" + transactionNotifyEvent.getUserName()); }
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) public void doSomething2(TransactionNotifyEvent transactionNotifyEvent) { System.out.println("提交之后" + transactionNotifyEvent.getUserName()); }
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, fallbackExecution = true) public void doSomething3(TransactionNotifyEvent transactionNotifyEvent) { System.out.println("提交完成" + transactionNotifyEvent.getUserName()); }
@TransactionalEventListener public void afterCommit(TransactionalEvent transactionalEvent){ allEvents.add(transactionalEvent); }
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT) public void beforeCommit(TransactionalEvent transactionalEvent){ allEvents.add(transactionalEvent); }
@TransactionalEventListener public void handleSaleEvent(SaleCreatedEvent event) { handleSaleModificationEvent(event); }
@TransactionalEventListener public void handleSaleEvent(SaleUpdatedEvent event) { handleSaleModificationEvent(event); }
@TransactionalEventListener public void handleSaleDeleted(SaleDeletedEvent event) { this.template.convertAndSend("/topic/deletedSale", event.getSale().getId()); }
@TransactionalEventListener() public void handleAfterTxCommitTask(AfterTxCommitTask task) { task.run(); }
@TransactionalEventListener public void blogAddedTransactional(BlogAddedEvent blogAddedEvent) { log.info("Transactional sending external notification for added blog: {}", blogAddedEvent.getBlog()); notificationSender.blogAddedTransactional(blogAddedEvent); }
@TransactionalEventListener(fallbackExecution = true) public void blogAddedTransactionalWithFallback(BlogAddedEvent blogAddedEvent) { log.info("Transactional (with fallback) sending external notification for added blog: {}", blogAddedEvent.getBlog()); notificationSender.blogAddedTransactionalWithFallback(blogAddedEvent); }