001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.messagedriven.beans;
023:
024: import java.util.Enumeration;
025: import java.util.Properties;
026:
027: import javax.ejb.MessageDrivenBean;
028: import javax.ejb.MessageDrivenContext;
029: import javax.jms.JMSException;
030: import javax.jms.Message;
031: import javax.jms.MessageListener;
032: import javax.transaction.Transaction;
033:
034: import org.jboss.ejb.plugins.jms.DLQHandler;
035: import org.jboss.logging.Logger;
036: import org.jboss.mx.util.MBeanProxyExt;
037: import org.jboss.test.messagedriven.mbeans.TestMessageDrivenManagementMBean;
038:
039: /**
040: * A Test Message Driven Bean
041: *
042: * @author <a href="mailto:adrian@jboss.com>Adrian Brock</a>
043: * @version <tt>$Revision: 1.4</tt>
044: */
045: public class TestMessageDriven implements MessageDrivenBean,
046: MessageListener {
047: /** The serialVersionUID */
048: private static final long serialVersionUID = 1L;
049:
050: protected static final Logger log = Logger
051: .getLogger(TestMessageDriven.class);
052:
053: protected MessageDrivenContext ctx;
054: protected TestMessageDrivenManagementMBean mbean;
055:
056: public void onMessage(Message message) {
057: log.info("Got message: " + message);
058: mbean.addMessage(message);
059: if (isDLQ(message))
060: return;
061: logProperties();
062: logTransaction();
063: String rollback = getRollback();
064: if (rollback.equals("DLQ")) {
065: log.info("Rollback DLQ");
066: ctx.setRollbackOnly();
067: }
068: }
069:
070: public boolean isDLQ(Message message) {
071: try {
072: if (message
073: .getStringProperty(DLQHandler.JBOSS_ORIG_DESTINATION) != null)
074: return true;
075: } catch (JMSException e) {
076: log.error("Unhandled error", e);
077: }
078: return false;
079: }
080:
081: public String getRollback() {
082: return System
083: .getProperty("test.messagedriven.rollback", "None");
084: }
085:
086: public void logProperties() {
087: Properties props = System.getProperties();
088: for (Enumeration e = props.keys(); e.hasMoreElements();) {
089: String key = (String) e.nextElement();
090: if (key.startsWith("test.messagedriven."))
091: log.info(key + "=" + props.getProperty(key));
092: }
093: }
094:
095: public Transaction logTransaction() {
096: Transaction tx = mbean.getTransaction();
097: log.info("tx=" + tx);
098: return tx;
099: }
100:
101: public void ejbCreate() {
102: mbean = (TestMessageDrivenManagementMBean) MBeanProxyExt
103: .create(TestMessageDrivenManagementMBean.class,
104: TestMessageDrivenManagementMBean.OBJECT_NAME);
105: }
106:
107: public void ejbRemove() {
108: }
109:
110: public void setMessageDrivenContext(MessageDrivenContext ctx) {
111: this.ctx = ctx;
112: }
113: }
|