[seam-commits] Seam SVN: r12671 - in modules/jms/branches: simple-forwarding/api/src/main/java/org/jboss/seam/jms/annotations and 4 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Fri Apr 30 20:35:53 EDT 2010


Author: jganoff
Date: 2010-04-30 20:35:52 -0400 (Fri, 30 Apr 2010)
New Revision: 12671

Added:
   modules/jms/branches/simple-forwarding/
   modules/jms/branches/simple-forwarding/api/src/main/java/org/jboss/seam/jms/annotations/Bridged.java
   modules/jms/branches/simple-forwarding/impl/src/main/java/org/jboss/seam/jms/BridgedObserver.java
   modules/jms/branches/simple-forwarding/impl/src/test/java/org/jboss/seam/jms/test/bridge/
   modules/jms/branches/simple-forwarding/impl/src/test/java/org/jboss/seam/jms/test/bridge/simple/
   modules/jms/branches/simple-forwarding/impl/src/test/java/org/jboss/seam/jms/test/bridge/simple/MyTopic.java
   modules/jms/branches/simple-forwarding/impl/src/test/java/org/jboss/seam/jms/test/bridge/simple/SimpleBridgedEventTest.java
Modified:
   modules/jms/branches/simple-forwarding/impl/src/main/java/org/jboss/seam/jms/Seam3JmsExtension.java
Log:
Initial ideas on simple event forwarding

Copied: modules/jms/branches/simple-forwarding (from rev 12664, modules/jms/trunk)

Added: modules/jms/branches/simple-forwarding/api/src/main/java/org/jboss/seam/jms/annotations/Bridged.java
===================================================================
--- modules/jms/branches/simple-forwarding/api/src/main/java/org/jboss/seam/jms/annotations/Bridged.java	                        (rev 0)
+++ modules/jms/branches/simple-forwarding/api/src/main/java/org/jboss/seam/jms/annotations/Bridged.java	2010-05-01 00:35:52 UTC (rev 12671)
@@ -0,0 +1,28 @@
+package org.jboss.seam.jms.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.inject.Qualifier;
+
+/**
+ * Bridged Event
+ * 
+ * @author Jordan Ganoff
+ */
+ at Qualifier
+ at Documented
+ at Inherited
+ at Target( { FIELD, METHOD, TYPE, PARAMETER })
+ at Retention(RUNTIME)
+public @interface Bridged
+{
+}
\ No newline at end of file

Added: modules/jms/branches/simple-forwarding/impl/src/main/java/org/jboss/seam/jms/BridgedObserver.java
===================================================================
--- modules/jms/branches/simple-forwarding/impl/src/main/java/org/jboss/seam/jms/BridgedObserver.java	                        (rev 0)
+++ modules/jms/branches/simple-forwarding/impl/src/main/java/org/jboss/seam/jms/BridgedObserver.java	2010-05-01 00:35:52 UTC (rev 12671)
@@ -0,0 +1,108 @@
+package org.jboss.seam.jms;
+
+import java.io.Serializable;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.Collections;
+import java.util.Set;
+
+import javax.enterprise.event.Reception;
+import javax.enterprise.event.TransactionPhase;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.ObserverMethod;
+import javax.enterprise.util.AnnotationLiteral;
+import javax.jms.Destination;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.Session;
+
+import org.jboss.seam.jms.annotations.Bridged;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Observer Method to observe events and forward (bridge) them over JMS.
+ * 
+ * @author Jordan Ganoff
+ */
+public class BridgedObserver implements ObserverMethod<Object>
+{
+   private Logger log = LoggerFactory.getLogger(getClass());
+
+   private static final Set<Annotation> BRIDGED = Collections.<Annotation> singleton(new AnnotationLiteral<Bridged>()
+   {
+      private static final long serialVersionUID = 1L;
+   });
+
+   private BeanManager bm;
+   private Destination destination;
+   private Set<Annotation> qualifiers;
+
+   public BridgedObserver(BeanManager bm)
+   {
+      this.bm = bm;
+   }
+
+   public Class<?> getBeanClass()
+   {
+      return null;
+   }
+
+   public Set<Annotation> getObservedQualifiers()
+   {
+      // Note, annotation MUST be @Qualifier. No error given but will not work!
+      return BRIDGED;
+   }
+
+   public Type getObservedType()
+   {
+      return Object.class;
+   }
+
+   public Reception getReception()
+   {
+      return Reception.ALWAYS;
+   }
+
+   public TransactionPhase getTransactionPhase()
+   {
+      return TransactionPhase.AFTER_SUCCESS;
+   }
+
+   public void notify(Object evt)
+   {
+      // FIXME Include qualifiers once CDI 1.0 MR is complete and
+      // notify(Event, Set<Annotation>) is added
+      forwardEvent(evt, null);
+   }
+
+   private void forwardEvent(Object event, Set<Annotation> qualifiers)
+   {
+      log.info("Forwarding event {}", event);
+      // p.forwardEvent(event, qualifiers);
+
+      // for (EventPropagator p : ServiceLoader.load(EventPropagator.class))
+      // {
+      // p.forwardEvent(event, null);
+      // }
+
+      try
+      {
+         Set<Bean<?>> beans = bm.getBeans(Session.class);// , new
+         // AnnotationLiteral<org.jboss.seam.jms.annotations.Session>()
+         // {});
+         Bean<?> bean = bm.resolve(beans);
+         Session s = (Session) bm.getReference(bean, Session.class, bm.createCreationalContext(bean));
+         log.info("Forwarding with session {}", s);
+         log.info("To destination {}", destination);
+         Message m = s.createObjectMessage((Serializable) event);
+         s.createProducer(destination).send(m);
+         s.close();
+      }
+      catch (JMSException ex)
+      {
+         log.error("Unable to forward event", ex);
+      }
+   }
+}

Modified: modules/jms/branches/simple-forwarding/impl/src/main/java/org/jboss/seam/jms/Seam3JmsExtension.java
===================================================================
--- modules/jms/trunk/impl/src/main/java/org/jboss/seam/jms/Seam3JmsExtension.java	2010-04-29 20:57:43 UTC (rev 12664)
+++ modules/jms/branches/simple-forwarding/impl/src/main/java/org/jboss/seam/jms/Seam3JmsExtension.java	2010-05-01 00:35:52 UTC (rev 12671)
@@ -1,6 +1,8 @@
 package org.jboss.seam.jms;
 
 import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.Extension;
 import javax.enterprise.inject.spi.ProcessAnnotatedType;
 
@@ -13,8 +15,18 @@
  */
 public class Seam3JmsExtension implements Extension
 {
+   public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm)
+   {
+      // Temporary creation of bridged overserver (This should be changed to allow configuration from XML)
+      BridgedObserver b = new BridgedObserver(bm);
+      abd.addObserverMethod(b);
+   }
+   
    public <X> void decorateAnnotatedType(@Observes ProcessAnnotatedType<X> pat)
    {
+      /**
+       * Flatten all @Annotated that define @JmsDestinations so that they may be injected  
+       */
       pat.setAnnotatedType(JmsAnnotatedTypeWrapper.decorate(pat.getAnnotatedType()));
    }
 }

Added: modules/jms/branches/simple-forwarding/impl/src/test/java/org/jboss/seam/jms/test/bridge/simple/MyTopic.java
===================================================================
--- modules/jms/branches/simple-forwarding/impl/src/test/java/org/jboss/seam/jms/test/bridge/simple/MyTopic.java	                        (rev 0)
+++ modules/jms/branches/simple-forwarding/impl/src/test/java/org/jboss/seam/jms/test/bridge/simple/MyTopic.java	2010-05-01 00:35:52 UTC (rev 12671)
@@ -0,0 +1,12 @@
+package org.jboss.seam.jms.test.bridge.simple;
+
+import javax.inject.Qualifier;
+
+import org.jboss.seam.jms.annotations.JmsDestination;
+
+ at Qualifier
+ at JmsDestination(jndiName="jms/T")
+public @interface MyTopic
+{
+
+}

Added: modules/jms/branches/simple-forwarding/impl/src/test/java/org/jboss/seam/jms/test/bridge/simple/SimpleBridgedEventTest.java
===================================================================
--- modules/jms/branches/simple-forwarding/impl/src/test/java/org/jboss/seam/jms/test/bridge/simple/SimpleBridgedEventTest.java	                        (rev 0)
+++ modules/jms/branches/simple-forwarding/impl/src/test/java/org/jboss/seam/jms/test/bridge/simple/SimpleBridgedEventTest.java	2010-05-01 00:35:52 UTC (rev 12671)
@@ -0,0 +1,56 @@
+package org.jboss.seam.jms.test.bridge.simple;
+
+import javax.enterprise.event.Event;
+import javax.inject.Inject;
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.ObjectMessage;
+import javax.jms.Session;
+import javax.jms.Topic;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.seam.jms.annotations.Bridged;
+import org.jboss.seam.jms.annotations.JmsDestination;
+import org.jboss.seam.jms.test.Util;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+ at RunWith(Arquillian.class)
+public class SimpleBridgedEventTest
+{
+   @Deployment
+   public static JavaArchive createDeployment()
+   {
+      return Util.createDeployment(SimpleBridgedEventTest.class);
+   }
+   
+   @Inject Connection c;
+   @Inject Session s;
+   @Inject @JmsDestination(jndiName="jms/T") Topic t;
+   
+   @Inject @Bridged Event<String> event;
+   
+   @Test
+   public void a()
+   {
+      
+   }
+   
+   @Test
+   public void forwardSimpleEvent() throws JMSException
+   {
+      String expected = "test";
+      MessageConsumer mc = s.createConsumer(t);
+      c.start();
+      event.fire(expected);
+      Message m = mc.receive();
+      Assert.assertTrue(m != null);
+      Assert.assertTrue(m instanceof ObjectMessage);
+      Assert.assertEquals(expected, ((ObjectMessage) m).getObject());
+   }
+}



More information about the seam-commits mailing list