1 18 package org.apache.activemq.systest.impl; 19 20 import org.apache.activemq.systest.AgentStopper; 21 import org.apache.activemq.systest.MessageList; 22 import org.apache.activemq.systest.ProducerAgent; 23 24 import javax.jms.DeliveryMode ; 25 import javax.jms.JMSException ; 26 import javax.jms.MessageProducer ; 27 28 33 public class ProducerAgentImpl extends JmsClientSupport implements ProducerAgent { 34 35 private MessageProducer producer; 36 private boolean persistent = true; 37 38 public void start() throws Exception { 39 super.start(); 40 producer = createProducer(); 41 } 42 43 public void sendMessages(MessageList messageList) throws JMSException { 44 System.out.println("About to send: " + messageList.getSize() + " message(s) to destination: " + getDestination()); 45 messageList.sendMessages(getSession(), producer); 46 if (isTransacted()) { 47 getSession().commit(); 48 } 49 System.out.println("Sent: " + messageList.getSize() + " message(s) to destination: " + getDestination()); 50 } 51 52 public void sendMessages(MessageList messageList, int percent) throws JMSException { 53 System.out.println("About to send: " + percent + " % of the message(s) to destination: " + getDestination()); 54 messageList.sendMessages(getSession(), producer, percent); 55 if (isTransacted()) { 56 getSession().commit(); 57 } 58 System.out.println("Sent: " + percent + " % of the message(s) to destination: " + getDestination()); 59 } 60 61 public boolean isPersistent() { 62 return persistent; 63 } 64 65 public void setPersistent(boolean persistent) { 66 this.persistent = persistent; 67 } 68 69 public void stop(AgentStopper stopper) { 70 if (producer != null) { 71 try { 72 producer.close(); 73 } 74 catch (JMSException e) { 75 stopper.onException(this, e); 76 } 77 finally { 78 producer = null; 79 } 80 } 81 super.stop(stopper); 82 } 83 84 protected MessageProducer createProducer() throws JMSException { 87 MessageProducer answer = getSession().createProducer(getDestination()); 88 answer.setDeliveryMode(isPersistent() ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT); 89 return answer; 90 } 91 92 } 93 | Popular Tags |