javax.enterprise.event.TransactionPhase Java Examples

The following examples show how to use javax.enterprise.event.TransactionPhase. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ObserverInfo.java    From quarkus with Apache License 2.0 6 votes vote down vote up
ObserverInfo(BeanDeployment beanDeployment, DotName beanClass, BeanInfo declaringBean, MethodInfo observerMethod,
        Injection injection,
        MethodParameterInfo eventParameter,
        boolean isAsync, int priority, Reception reception, TransactionPhase transactionPhase,
        Type observedType, Set<AnnotationInstance> qualifiers, Consumer<MethodCreator> notify) {
    this.beanDeployment = beanDeployment;
    this.beanClass = beanClass;
    this.declaringBean = declaringBean;
    this.observerMethod = observerMethod;
    this.injection = injection;
    this.eventParameter = eventParameter;
    this.eventMetadataParameterPosition = initEventMetadataParam(observerMethod);
    this.isAsync = isAsync;
    this.priority = priority;
    this.reception = reception;
    this.transactionPhase = transactionPhase;
    this.observedType = observedType;
    this.qualifiers = qualifiers;
    this.notify = notify;
}
 
Example #2
Source File: EventPublisherTest.java    From rabbitmq-cdi with MIT License 6 votes vote down vote up
/**
 * Test method for {@link EventPublisher#addEvent(EventKey, PublisherConfiguration)},
 * {@link EventPublisher#publishEvent(Object, TransactionPhase)} and
 * {@link EventPublisher#cleanUp()}.
 * 
 * @throws TimeoutException
 * @throws IOException
 */
@Test
public void testPublishEvent_failing() throws IOException, TimeoutException {
  EventKey<TestEvent> key = EventKey.of(TestEvent.class, TransactionPhase.AFTER_FAILURE);

  when(connectionRepository.getConnection(config)).thenReturn(connection);
  when(connection.createChannel()).thenReturn(channel);
  doThrow(IOException.class).when(channel).basicPublish(eq("exchange"), eq("routingKey"), any(),
      any());

  publisher.addEvent(key, new PublisherConfiguration(config, "exchange", routingKeyFunction,
      basicProperties, encoder, errorHandler, declarations));
  publisher.publishEvent(new TestEvent(), TransactionPhase.AFTER_FAILURE);
  publisher.cleanUp();

  verify(channel, times(3)).close();
}
 
Example #3
Source File: EventKeyTest.java    From rabbitmq-cdi with MIT License 6 votes vote down vote up
/**
 * Test method for {@link EventKey#equals(Object)}.
 */
@Test
void testEqualsObject() {
  assertNotEquals(eventKey, null);
  assertNotEquals(eventKey, new Object());
  assertNotEquals(eventKey, EventKey.of(Object.class, TransactionPhase.IN_PROGRESS));
  assertNotEquals(eventKey, EventKey.of(TestEvent.class, TransactionPhase.AFTER_COMPLETION));

  assertEquals(eventKey, eventKey);

  EventKey<TestEvent> eventKey1 = EventKey.of(TestEvent.class, TransactionPhase.IN_PROGRESS);
  assertEquals(eventKey, eventKey1);
  assertEquals(eventKey1, eventKey);

  EventKey<TestEvent> eventKey2 = EventKey.of(TestEvent.class, TransactionPhase.IN_PROGRESS);
  assertEquals(eventKey, eventKey2);

  assertEquals(eventKey1, eventKey2);
  assertEquals(eventKey1.hashCode(), eventKey2.hashCode());
}
 
Example #4
Source File: AuditLogServiceBeanIT.java    From development with Apache License 2.0 6 votes vote down vote up
@Test
public void saveAuditLogEntries_hasObserverMethodAfterSuccessAnnotation() {
    // given
    AuditLogEntries auditLogEntries = new AuditLogEntries(
            new ArrayList<AuditLogEntry>());
    List<ObserverMethod> observerMethods = contextManager
            .findObserverMethods(auditLogEntries);
    Method method = observerMethods.get(0).getMethod();

    // when
    Observes observes = (Observes) contextManager.searchAnnotation(method,
            Observes.class);

    // then
    assertEquals(TransactionPhase.AFTER_SUCCESS, observes.during());
}
 
Example #5
Source File: OpenEJBTransactionService.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public void registerTransactionSynchronization(final TransactionPhase phase, final ObserverMethod<? super Object> observer, final Object event) throws Exception {
    Set<Annotation> qualifiers = observer.getObservedQualifiers();
    if (qualifiers == null) {
        qualifiers = Collections.emptySet();
    }

    TransactionalEventNotifier.registerTransactionSynchronization(phase, observer, event,
        new EventMetadataImpl(observer.getObservedType(), null, null,
            qualifiers.toArray(new Annotation[qualifiers.size()]), webBeansContext));
}
 
Example #6
Source File: EventPublisherTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
@Test
public void testOnEventAfterSuccess() throws IOException, TimeoutException {
  EventKey<TestEvent> key = EventKey.of(TestEvent.class, TransactionPhase.AFTER_SUCCESS);

  when(connectionRepository.getConnection(config)).thenReturn(connection);
  when(connection.createChannel()).thenReturn(channel);

  publisher.addEvent(key, new PublisherConfiguration(config, "exchange", routingKeyFunction,
      basicProperties, encoder, errorHandler, declarations));
  publisher.onEventAfterSuccess(new TestEvent());
  publisher.cleanUp();

  verify(channel).basicPublish(eq("exchange"), eq("routingKey"), any(), any());
  verify(channel).close();
}
 
Example #7
Source File: EventPublisherTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
@Test
public void testOnEventAfterFailure() throws IOException, TimeoutException {
  EventKey<TestEvent> key = EventKey.of(TestEvent.class, TransactionPhase.AFTER_FAILURE);

  when(connectionRepository.getConnection(config)).thenReturn(connection);
  when(connection.createChannel()).thenReturn(channel);

  publisher.addEvent(key, new PublisherConfiguration(config, "exchange", routingKeyFunction,
      basicProperties, encoder, errorHandler, declarations));
  publisher.onEventAfterFailure(new TestEvent());
  publisher.cleanUp();

  verify(channel).basicPublish(eq("exchange"), eq("routingKey"), any(), any());
  verify(channel).close();
}
 
Example #8
Source File: EventPublisherTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
@Test
public void testOnEventAfterCompletion() throws IOException, TimeoutException {
  EventKey<TestEvent> key = EventKey.of(TestEvent.class, TransactionPhase.AFTER_COMPLETION);

  when(connectionRepository.getConnection(config)).thenReturn(connection);
  when(connection.createChannel()).thenReturn(channel);

  publisher.addEvent(key, new PublisherConfiguration(config, "exchange", routingKeyFunction,
      basicProperties, encoder, errorHandler, declarations));
  publisher.onEventAfterCompletion(new TestEvent());
  publisher.cleanUp();

  verify(channel).basicPublish(eq("exchange"), eq("routingKey"), any(), any());
  verify(channel).close();
}
 
Example #9
Source File: EventPublisherTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
@Test
public void testOnEventInBeforeCompletion() throws IOException, TimeoutException {
  EventKey<TestEvent> key = EventKey.of(TestEvent.class, TransactionPhase.BEFORE_COMPLETION);

  when(connectionRepository.getConnection(config)).thenReturn(connection);
  when(connection.createChannel()).thenReturn(channel);

  publisher.addEvent(key, new PublisherConfiguration(config, "exchange", routingKeyFunction,
      basicProperties, encoder, errorHandler, declarations));
  publisher.onEventBeforeCompletion(new TestEvent());
  publisher.cleanUp();

  verify(channel).basicPublish(eq("exchange"), eq("routingKey"), any(), any());
  verify(channel).close();
}
 
Example #10
Source File: EventPublisherTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
@Test
public void testOnEventInProgress() throws IOException, TimeoutException {
  EventKey<TestEvent> key = EventKey.of(TestEvent.class, TransactionPhase.IN_PROGRESS);

  when(connectionRepository.getConnection(config)).thenReturn(connection);
  when(connection.createChannel()).thenReturn(channel);

  publisher.addEvent(key, new PublisherConfiguration(config, "exchange", routingKeyFunction,
      basicProperties, encoder, errorHandler, declarations));
  publisher.onEventInProgress(new TestEvent());
  publisher.cleanUp();

  verify(channel).basicPublish(eq("exchange"), eq("routingKey"), any(), any());
  verify(channel).close();
}
 
Example #11
Source File: EventPublisherTest.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
/**
 * Test method for {@link EventPublisher#addEvent(EventKey, PublisherConfiguration)},
 * {@link EventPublisher#publishEvent(Object, TransactionPhase)} and
 * {@link EventPublisher#cleanUp()}.
 * 
 * @throws TimeoutException
 * @throws IOException
 */
@Test
public void testPublishEvent() throws IOException, TimeoutException {
  EventKey<TestEvent> key = EventKey.of(TestEvent.class, TransactionPhase.AFTER_SUCCESS);
  when(connectionRepository.getConnection(config)).thenReturn(connection);
  when(connection.createChannel()).thenReturn(channel);

  publisher.addEvent(key, new PublisherConfiguration(config, "exchange", routingKeyFunction,
      basicProperties, encoder, errorHandler, declarations));
  publisher.publishEvent(new TestEvent(), TransactionPhase.AFTER_SUCCESS);
  publisher.cleanUp();

  verify(channel).basicPublish(eq("exchange"), eq("routingKey"), any(), any());
  verify(channel).close();
}
 
Example #12
Source File: EventPublisher.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
void publishEvent(Object event, TransactionPhase transactionPhase) {
  @SuppressWarnings("unchecked")
  EventKey<Object> eventKey =  (EventKey<Object>) EventKey.of(event.getClass(), transactionPhase);
  @SuppressWarnings("unchecked")
  PublisherConfiguration<Object> configuration = (PublisherConfiguration<Object>) publisherConfigurations.get(eventKey);
  if (configuration == null) {
    LOGGER.trace("No publisher configured for event {}", event);
  } else {
    doPublish(event, providePublisher(eventKey, transactionPhase), configuration);
  }
}
 
Example #13
Source File: EventBinder.java    From rabbitmq-cdi with MIT License 5 votes vote down vote up
ExchangeBinding(Class<T> eventType, String exchange) {
  this.eventType = eventType;
  this.exchange = exchange;
  this.headers = new HashMap<>();
  this.encoder = new JsonEncoder<>();
  routingKeyFunction = e -> "";
  transactionPhase = TransactionPhase.IN_PROGRESS;
  errorHandler = nop();
  basicPropertiesBuilder = MessageProperties.BASIC.builder().headers(headers);
  LOGGER.info("Binding created between exchange {} and event type {}", exchange,
      eventType.getSimpleName());
}
 
Example #14
Source File: ObserverInfo.java    From quarkus with Apache License 2.0 5 votes vote down vote up
static TransactionPhase initTransactionPhase(boolean isAsync, BeanDeployment beanDeployment, MethodInfo observerMethod) {
    AnnotationInstance observesAnnotation = isAsync
            ? beanDeployment.getAnnotation(observerMethod, DotNames.OBSERVES_ASYNC)
            : beanDeployment.getAnnotation(observerMethod, DotNames.OBSERVES);
    AnnotationValue duringValue = observesAnnotation.value("during");
    if (duringValue == null) {
        return TransactionPhase.IN_PROGRESS;
    }
    return TransactionPhase.valueOf(duringValue.asEnum());
}
 
Example #15
Source File: ObserverInfo.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public ObserverTransformationContext(BuildContext buildContext, AnnotationTarget target,
        Type observedType, Set<AnnotationInstance> qualifiers, Reception reception, TransactionPhase transactionPhase,
        Integer priority, boolean async) {
    super(buildContext, target, qualifiers);
    this.observedType = observedType;
    this.reception = reception;
    this.transactionPhase = transactionPhase;
    this.priority = priority;
    this.async = async;
}
 
Example #16
Source File: ObserverConfigurator.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public ObserverConfigurator(Consumer<ObserverConfigurator> consumer) {
    this.consumer = consumer;
    this.observedQualifiers = new HashSet<>();
    this.priority = ObserverMethod.DEFAULT_PRIORITY;
    this.isAsync = false;
    this.transactionPhase = TransactionPhase.IN_PROGRESS;
}
 
Example #17
Source File: EventImpl.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public static Status valueOf(TransactionPhase transactionPhase) {
    if (transactionPhase == TransactionPhase.BEFORE_COMPLETION
            || transactionPhase == TransactionPhase.AFTER_COMPLETION) {
        return Status.ALL;
    }
    if (transactionPhase == TransactionPhase.AFTER_SUCCESS) {
        return Status.SUCCESS;
    }
    if (transactionPhase == TransactionPhase.AFTER_FAILURE) {
        return Status.FAILURE;
    }
    throw new IllegalArgumentException("Unknown transaction phase " + transactionPhase);
}
 
Example #18
Source File: EventImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
DeferredEventNotification(ObserverMethod<? super T> observerMethod, EventContext eventContext, Status status) {
    this.observerMethod = observerMethod;
    this.isBeforeCompletion = observerMethod.getTransactionPhase().equals(TransactionPhase.BEFORE_COMPLETION);
    this.eventContext = eventContext;
    this.status = status;
}
 
Example #19
Source File: ObserverInfo.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public TransactionPhase getTransactionPhase() {
    return transactionPhase;
}
 
Example #20
Source File: ObserverTestBean.java    From hibernate-demos with Apache License 2.0 4 votes vote down vote up
public void observesShort(@Observes(during=TransactionPhase.IN_PROGRESS) Short event) {
    sb.append(event);
}
 
Example #21
Source File: ObserverTestBean.java    From hibernate-demos with Apache License 2.0 4 votes vote down vote up
public void observeInteger(@Observes(during=TransactionPhase.BEFORE_COMPLETION) Integer event) {
    sb.append(event);
}
 
Example #22
Source File: ObserverTestBean.java    From hibernate-demos with Apache License 2.0 4 votes vote down vote up
public void observesLong(@Observes(during=TransactionPhase.AFTER_COMPLETION) Long event) {
    sb.append(event);
}
 
Example #23
Source File: ExchangeBindingTest.java    From rabbitmq-cdi with MIT License 4 votes vote down vote up
@Test
void inPhase() {
  assertEquals(TransactionPhase.IN_PROGRESS, binding.getTransactionPhase());
  assertSame(binding, binding.inPhase(TransactionPhase.AFTER_COMPLETION));
  assertEquals(TransactionPhase.AFTER_COMPLETION, binding.getTransactionPhase());
}
 
Example #24
Source File: ObserverInfo.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public TransactionPhase getTransactionPhase() {
    return transactionPhase;
}
 
Example #25
Source File: ObserverGenerator.java    From quarkus with Apache License 2.0 4 votes vote down vote up
protected void implementGetTransactionPhase(ClassCreator observerCreator, ObserverInfo observer) {
    MethodCreator getTransactionPhase = observerCreator.getMethodCreator("getTransactionPhase", TransactionPhase.class)
            .setModifiers(ACC_PUBLIC);
    getTransactionPhase.returnValue(getTransactionPhase.load(observer.getTransactionPhase()));
}
 
Example #26
Source File: EventImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private boolean isTxObserver(ObserverMethod<?> observer) {
    return !observer.getTransactionPhase().equals(TransactionPhase.IN_PROGRESS);
}
 
Example #27
Source File: EventPublisherTest.java    From rabbitmq-cdi with MIT License 4 votes vote down vote up
/**
 * Test method for {@link EventPublisher#publishEvent(Object, TransactionPhase)}.
 */
@Test
public void testPublishEvent_no_configuration() {
  publisher.publishEvent(new TestEvent(), TransactionPhase.AFTER_COMPLETION);
}
 
Example #28
Source File: EventImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private boolean isNotAfterSuccess(ObserverMethod<?> observer) {
    return !observer.getTransactionPhase().equals(TransactionPhase.AFTER_SUCCESS);
}
 
Example #29
Source File: EventKeyTest.java    From rabbitmq-cdi with MIT License 4 votes vote down vote up
@BeforeEach
void prepare() {
  eventKey = EventKey.of(TestEvent.class, TransactionPhase.IN_PROGRESS);
}
 
Example #30
Source File: AuditLogServiceBean.java    From development with Apache License 2.0 4 votes vote down vote up
public void saveAuditLogEntries(
        @Observes(during = TransactionPhase.AFTER_SUCCESS) AuditLogEntries logData) {
    dao.saveAuditLog(logData.getAuditLogEntries());
}