KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > messagedriven > beans > TestMessageDriven


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.messagedriven.beans;
23
24 import java.util.Enumeration JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import javax.ejb.MessageDrivenBean JavaDoc;
28 import javax.ejb.MessageDrivenContext JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30 import javax.jms.Message JavaDoc;
31 import javax.jms.MessageListener JavaDoc;
32 import javax.transaction.Transaction JavaDoc;
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 /**
40  * A Test Message Driven Bean
41  *
42  * @author <a HREF="mailto:adrian@jboss.com>Adrian Brock</a>
43  * @version <tt>$Revision: 1.4</tt>
44  */

45 public class TestMessageDriven implements MessageDrivenBean JavaDoc, MessageListener JavaDoc
46 {
47    /** The serialVersionUID */
48    private static final long serialVersionUID = 1L;
49
50    protected static final Logger log = Logger.getLogger(TestMessageDriven.class);
51    
52    protected MessageDrivenContext JavaDoc ctx;
53    protected TestMessageDrivenManagementMBean mbean;
54    
55    public void onMessage(Message JavaDoc message)
56    {
57       log.info("Got message: " + message);
58       mbean.addMessage(message);
59       if (isDLQ(message))
60          return;
61       logProperties();
62       logTransaction();
63       String JavaDoc rollback = getRollback();
64       if (rollback.equals("DLQ"))
65       {
66          log.info("Rollback DLQ");
67          ctx.setRollbackOnly();
68       }
69    }
70    
71    public boolean isDLQ(Message JavaDoc message)
72    {
73       try
74       {
75          if (message.getStringProperty(DLQHandler.JBOSS_ORIG_DESTINATION) != null)
76             return true;
77       }
78       catch (JMSException JavaDoc e)
79       {
80          log.error("Unhandled error", e);
81       }
82       return false;
83    }
84    
85    public String JavaDoc getRollback()
86    {
87       return System.getProperty("test.messagedriven.rollback", "None");
88    }
89    
90    public void logProperties()
91    {
92       Properties JavaDoc props = System.getProperties();
93       for (Enumeration JavaDoc e = props.keys(); e.hasMoreElements();)
94       {
95          String JavaDoc key = (String JavaDoc) e.nextElement();
96          if (key.startsWith("test.messagedriven."))
97             log.info(key + "=" + props.getProperty(key));
98       }
99    }
100    
101    public Transaction JavaDoc logTransaction()
102    {
103       Transaction JavaDoc 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 JavaDoc ctx)
118    {
119       this.ctx = ctx;
120    }
121 }
122
Popular Tags