Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » systest » impl » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.systest.impl;
   18   
   19   import org.apache.activemq.systest.AgentStopper;
   20   import org.apache.activemq.systest.MessageList;
   21   import org.apache.activemq.systest.ProducerAgent;
   22   
   23   import javax.jms.DeliveryMode;
   24   import javax.jms.JMSException;
   25   import javax.jms.MessageProducer;
   26   
   27   /**
   28    * A simple in JVM implementation of a {@link ProducerAgent}
   29    * 
   30    * @version $Revision: 1.1 $
   31    */
   32   public class ProducerAgentImpl extends JmsClientSupport implements ProducerAgent {
   33   
   34       private MessageProducer producer;
   35       private boolean persistent = true;
   36   
   37       public void start() throws Exception {
   38           super.start();
   39           producer = createProducer();
   40       }
   41   
   42       public void sendMessages(MessageList messageList) throws JMSException {
   43           System.out.println("About to send: " + messageList.getSize() + " message(s) to destination: " + getDestination());
   44           messageList.sendMessages(getSession(), producer);
   45           if (isTransacted()) {
   46               getSession().commit();
   47           }
   48           System.out.println("Sent: " + messageList.getSize() + " message(s) to destination: " + getDestination());
   49       }
   50       
   51       public void sendMessages(MessageList messageList, int percent) throws JMSException {
   52           System.out.println("About to send: " + percent + " % of the message(s) to destination: " + getDestination());
   53           messageList.sendMessages(getSession(), producer, percent);
   54           if (isTransacted()) {
   55               getSession().commit();
   56           }
   57           System.out.println("Sent: " + percent + " % of the message(s) to destination: " + getDestination());
   58       }
   59   
   60       public boolean isPersistent() {
   61           return persistent;
   62       }
   63   
   64       public void setPersistent(boolean persistent) {
   65           this.persistent = persistent;
   66       }
   67   
   68       public void stop(AgentStopper stopper) {
   69           if (producer != null) {
   70               try {
   71                   producer.close();
   72               }
   73               catch (JMSException e) {
   74                   stopper.onException(this, e);
   75               }
   76               finally {
   77                   producer = null;
   78               }
   79           }
   80           super.stop(stopper);
   81       }
   82   
   83       // Implementation methods
   84       // -------------------------------------------------------------------------
   85       protected MessageProducer createProducer() throws JMSException {
   86           MessageProducer answer = getSession().createProducer(getDestination());
   87           answer.setDeliveryMode(isPersistent() ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
   88           return answer;
   89       }
   90   
   91   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » systest » impl » [javadoc | source]