/* * JBoss, Home of Professional Open Source * Copyright 2005-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. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.test.messaging.jms.stress; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.Session; import org.jboss.messaging.core.logging.Logger; /** * * A Receiver that receives messages from a destination in a JMS transaction * * Receives <commitSize> messages then commits, then * Receives <rollbackSize> messages then rollsback until * a total of <numMessages> messages have been received (committed) * <nuMessages> must be a multiple of <commitSize> * * @author <a href="tim.fox@jboss.com">Tim Fox</a> * @version <tt>$Revision$</tt> * * $Id$ */ public class TransactionalReceiver extends Receiver { private static final Logger log = Logger.getLogger(TransactionalReceiver.class); protected int commitSize; protected int rollbackSize; class Count { int lastCommitted; int lastReceived; } public TransactionalReceiver(Session sess, MessageConsumer cons, int numMessages, int commitSize, int rollbackSize, boolean isListener) throws Exception { super(sess, cons, numMessages, isListener); this.commitSize = commitSize; this.rollbackSize = rollbackSize; } public void run() { //Small pause so as not to miss any messages in a topic try { Thread.sleep(1000); } catch (InterruptedException e) { } try { int iterations = numMessages / commitSize; for (int outerCount = 0; outerCount < iterations; outerCount++) { for (int innerCount = 0; innerCount < commitSize; innerCount++) { Message m = getMessage(); if (m == null) { log.error("Message is null"); setFailed(true); return; } String prodName = m.getStringProperty("PROD_NAME"); Integer msgCount = new Integer(m.getIntProperty("MSG_NUMBER")); Count count = (Count)counts.get(prodName); if (count == null) { //First time if (msgCount.intValue() != 0) { log.error("First message from " + prodName + " is not 0, it is " + msgCount); setFailed(true); return; } else { count = new Count(); counts.put(prodName, count); } } else { if (count.lastCommitted != msgCount.intValue() - 1) { log.error("Message out of sequence for " + m.getJMSMessageID() + " " + prodName + ", expected " + (count.lastCommitted + 1) + ", actual " + msgCount); setFailed(true); return; } } count.lastCommitted = msgCount.intValue(); count.lastReceived = msgCount.intValue(); if (innerCount == commitSize -1) { sess.commit(); } processingDone(); } if (outerCount == iterations - 1) { break; } for (int innerCount = 0; innerCount < rollbackSize; innerCount++) { Message m = getMessage(); if (m == null) { log.error("Message is null"); setFailed(true); return; } String prodName = m.getStringProperty("PROD_NAME"); Integer msgCount = new Integer(m.getIntProperty("MSG_NUMBER")); Count count = (Count)counts.get(prodName); if (count == null) { // First time if (msgCount.intValue() != 0) { log.error("First message from " + prodName + " is not 0, it is " + msgCount); setFailed(true); return; } else { count = new Count(); count.lastCommitted = -1; counts.put(prodName, count); } } else { if (count.lastReceived != msgCount.intValue() - 1) { log.error("Message out of sequence"); setFailed(true); return; } } count.lastReceived = msgCount.intValue(); if ((innerCount == rollbackSize -1) && (outerCount != iterations - 1)) { //Don't roll back on the very last one sess.rollback(); } processingDone(); } } finished(); } catch (Exception e) { log.error("Failed to receive message", e); setFailed(true); } } }