Skip to content

Commit

Permalink
Added support for asynchronously called transactional observers; upda…
Browse files Browse the repository at this point in the history
…ted tests for transactional observers.

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@1816 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
drallen committed Mar 8, 2009
1 parent 1422b2c commit 1a8182f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 9 deletions.
@@ -0,0 +1,57 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.webbeans.event;

import org.jboss.webbeans.context.DependentContext;
import org.jboss.webbeans.log.Log;
import org.jboss.webbeans.log.Logging;

/**
* @author David Allen
*
*/
public class AsynchronousTransactionalEventNotification<T> extends DeferredEventNotification<T>
{
private static Log log = Logging.getLog(DeferredEventNotification.class);

public AsynchronousTransactionalEventNotification(T event, ObserverImpl<T> observer)
{
super(event, observer);
}

@Override
public void run()
{
// Let the event be deferred again as just an asynchronous event
DependentContext.INSTANCE.setActive(true);
try
{
log.trace("Sending event [" + event + "] asynchronously to transaction observer " + observer);
observer.sendEventAsynchronously(event);
}
catch (Exception e)
{
log.error("Failure while queuing observer for event [" + event + "]", e);
}
finally
{
DependentContext.INSTANCE.setActive(false);
}
}

}
Expand Up @@ -31,9 +31,9 @@ public class DeferredEventNotification<T> implements Runnable
private static Log log = Logging.getLog(DeferredEventNotification.class);

// The observer
private ObserverImpl<T> observer;
protected ObserverImpl<T> observer;
// The event object
private T event;
protected T event;

/**
* Creates a new deferred event notifier.
Expand All @@ -52,15 +52,22 @@ public void run()
DependentContext.INSTANCE.setActive(true);
try
{
log.debug("Sending event [" + event + "] directly to observer " + observer);
observer.sendEvent(event);
}
catch (RuntimeException e)
catch (Exception e)
{
log.error("Failure while notifying an observer of an event", e);
log.error("Failure while notifying an observer of event [" + event + "]", e);
}
finally
{
DependentContext.INSTANCE.setActive(false);
}
}

@Override
public String toString()
{
return "Deferred event [" + event + "] for [" + observer + "]";
}
}
Expand Up @@ -28,6 +28,8 @@
import javax.event.Observer;

import org.jboss.webbeans.context.DependentContext;
import org.jboss.webbeans.log.Log;
import org.jboss.webbeans.log.Logging;
import org.jboss.webbeans.util.Reflections;
import org.jboss.webbeans.util.Strings;
import org.jboss.webbeans.util.collections.ForwardingMap;
Expand All @@ -40,7 +42,8 @@
*/
public class EventManager
{

private static Log log = Logging.getLog(EventManager.class);

/**
* An event type -> observer list map
*/
Expand Down Expand Up @@ -142,6 +145,7 @@ public <T> void addObserver(Observer<T> observer, Class<T> eventType, Annotation
{
EventObserver<T> eventObserver = new EventObserver<T>(observer, eventType, bindings);
registeredObservers.put(eventType, eventObserver);
log.debug("Added observer " + observer + " observing event type " + eventType);
}

/**
Expand All @@ -160,11 +164,13 @@ public <T> Set<Observer<T>> getObservers(T event, Annotation... bindings)
{
for (EventObserver<?> observer : registeredObservers.get(clazz))
{
log.debug("Checking observer " + observer + " to see if it is interested in event [" + event + "]");
if (observer.isObserverInterested(bindings))
{
@SuppressWarnings("unchecked")
Observer<T> o = (Observer<T>) observer.getObserver();
interestedObservers.add(o);
log.debug("Added observer " + observer + " for event [" + event + "]");
}
}
}
Expand Down
Expand Up @@ -23,6 +23,7 @@
import javax.event.AfterTransactionCompletion;
import javax.event.AfterTransactionFailure;
import javax.event.AfterTransactionSuccess;
import javax.event.Asynchronously;
import javax.event.BeforeTransactionCompletion;
import javax.inject.DefinitionException;
import javax.inject.manager.Bean;
Expand Down Expand Up @@ -85,10 +86,10 @@ void registerTask(TransactionServices transactionServices, Runnable task)
*/
public static boolean isObserverMethodTransactional(AnnotatedMethod<?> observer)
{
boolean transactional = false;
if ((!observer.getAnnotatedParameters(BeforeTransactionCompletion.class).isEmpty()) || (!observer.getAnnotatedParameters(AfterTransactionCompletion.class).isEmpty()) || (!observer.getAnnotatedParameters(AfterTransactionSuccess.class).isEmpty()) || (!observer.getAnnotatedParameters(AfterTransactionFailure.class).isEmpty()))
boolean transactional = true;
if ((observer.getAnnotatedParameters(BeforeTransactionCompletion.class).isEmpty()) || (observer.getAnnotatedParameters(AfterTransactionCompletion.class).isEmpty()) || (observer.getAnnotatedParameters(AfterTransactionSuccess.class).isEmpty()) || (observer.getAnnotatedParameters(AfterTransactionFailure.class).isEmpty()))
{
transactional = true;
transactional = false;
}
return transactional;
}
Expand Down Expand Up @@ -165,7 +166,15 @@ else if (observationPhases.size() == 1)
*/
private void deferEvent(T event)
{
DeferredEventNotification<T> deferredEvent = new DeferredEventNotification<T>(event, this);
DeferredEventNotification<T> deferredEvent = null;
if (this.observerMethod.getAnnotatedParameters(Asynchronously.class).isEmpty())
{
deferredEvent = new DeferredEventNotification<T>(event, this);
}
else
{
deferredEvent = new AsynchronousTransactionalEventNotification<T>(event, this);
}
transactionObservationPhase.registerTask(manager.getTransactionServices(), deferredEvent);
}

Expand Down

0 comments on commit 1a8182f

Please sign in to comment.