1 22 package org.jboss.test.messagedriven.beans; 23 24 import java.util.Enumeration ; 25 import java.util.Properties ; 26 27 import javax.ejb.MessageDrivenBean ; 28 import javax.ejb.MessageDrivenContext ; 29 import javax.jms.JMSException ; 30 import javax.jms.Message ; 31 import javax.jms.MessageListener ; 32 import javax.transaction.Transaction ; 33 34 import org.jboss.ejb.plugins.jms.DLQHandler; 35 import org.jboss.logging.Logger; 36 import org.jboss.mx.util.MBeanProxyExt; 37 import org.jboss.test.messagedriven.mbeans.TestMessageDrivenManagementMBean; 38 39 45 public class TestMessageDriven implements MessageDrivenBean , MessageListener 46 { 47 48 private static final long serialVersionUID = 1L; 49 50 protected static final Logger log = Logger.getLogger(TestMessageDriven.class); 51 52 protected MessageDrivenContext ctx; 53 protected TestMessageDrivenManagementMBean mbean; 54 55 public void onMessage(Message message) 56 { 57 log.info("Got message: " + message); 58 mbean.addMessage(message); 59 if (isDLQ(message)) 60 return; 61 logProperties(); 62 logTransaction(); 63 String rollback = getRollback(); 64 if (rollback.equals("DLQ")) 65 { 66 log.info("Rollback DLQ"); 67 ctx.setRollbackOnly(); 68 } 69 } 70 71 public boolean isDLQ(Message message) 72 { 73 try 74 { 75 if (message.getStringProperty(DLQHandler.JBOSS_ORIG_DESTINATION) != null) 76 return true; 77 } 78 catch (JMSException e) 79 { 80 log.error("Unhandled error", e); 81 } 82 return false; 83 } 84 85 public String getRollback() 86 { 87 return System.getProperty("test.messagedriven.rollback", "None"); 88 } 89 90 public void logProperties() 91 { 92 Properties props = System.getProperties(); 93 for (Enumeration e = props.keys(); e.hasMoreElements();) 94 { 95 String key = (String ) e.nextElement(); 96 if (key.startsWith("test.messagedriven.")) 97 log.info(key + "=" + props.getProperty(key)); 98 } 99 } 100 101 public Transaction logTransaction() 102 { 103 Transaction tx = mbean.getTransaction(); 104 log.info("tx=" + tx); 105 return tx; 106 } 107 108 public void ejbCreate() 109 { 110 mbean = (TestMessageDrivenManagementMBean) MBeanProxyExt.create(TestMessageDrivenManagementMBean.class, TestMessageDrivenManagementMBean.OBJECT_NAME); 111 } 112 113 public void ejbRemove() 114 { 115 } 116 117 public void setMessageDrivenContext(MessageDrivenContext ctx) 118 { 119 this.ctx = ctx; 120 } 121 } 122 | Popular Tags |