1 18 package org.apache.activemq.tool; 19 20 import org.apache.activemq.tool.properties.JmsClientProperties; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import javax.jms.ConnectionFactory ; 25 import javax.jms.Connection ; 26 import javax.jms.Session ; 27 import javax.jms.JMSException ; 28 import javax.jms.Destination ; 29 import java.util.Enumeration ; 30 31 public abstract class AbstractJmsClient { 32 private static final Log log = LogFactory.getLog(AbstractJmsClient.class); 33 34 protected ConnectionFactory factory; 35 protected Connection jmsConnection; 36 protected Session jmsSession; 37 38 protected int destCount = 1, destIndex = 0; 39 protected String clientName = ""; 40 41 public AbstractJmsClient(ConnectionFactory factory) { 42 this.factory = factory; 43 } 44 45 abstract public JmsClientProperties getClient(); 46 abstract public void setClient(JmsClientProperties client); 47 48 public ConnectionFactory getFactory() { 49 return factory; 50 } 51 52 public void setFactory(ConnectionFactory factory) { 53 this.factory = factory; 54 } 55 56 public int getDestCount() { 57 return destCount; 58 } 59 60 public void setDestCount(int destCount) { 61 this.destCount = destCount; 62 } 63 64 public int getDestIndex() { 65 return destIndex; 66 } 67 68 public void setDestIndex(int destIndex) { 69 this.destIndex = destIndex; 70 } 71 72 public String getClientName() { 73 return clientName; 74 } 75 76 public void setClientName(String clientName) { 77 this.clientName = clientName; 78 } 79 80 public Connection getConnection() throws JMSException { 81 if (jmsConnection == null) { 82 jmsConnection = factory.createConnection(); 83 jmsConnection.setClientID(getClientName()); 84 log.info("Creating JMS Connection: Provider=" + getClient().getJmsProvider() + ", JMS Spec=" + getClient().getJmsVersion()); 85 } 86 return jmsConnection; 87 } 88 89 public Session getSession() throws JMSException { 90 if (jmsSession == null) { 91 int ackMode; 92 if (getClient().getSessAckMode().equalsIgnoreCase(JmsClientProperties.SESSION_AUTO_ACKNOWLEDGE)) { 93 ackMode = Session.AUTO_ACKNOWLEDGE; 94 } else if (getClient().getSessAckMode().equalsIgnoreCase(JmsClientProperties.SESSION_CLIENT_ACKNOWLEDGE)) { 95 ackMode = Session.CLIENT_ACKNOWLEDGE; 96 } else if (getClient().getSessAckMode().equalsIgnoreCase(JmsClientProperties.SESSION_DUPS_OK_ACKNOWLEDGE)) { 97 ackMode = Session.DUPS_OK_ACKNOWLEDGE; 98 } else if (getClient().getSessAckMode().equalsIgnoreCase(JmsClientProperties.SESSION_TRANSACTED)) { 99 ackMode = Session.SESSION_TRANSACTED; 100 } else { 101 ackMode = Session.AUTO_ACKNOWLEDGE; 102 } 103 jmsSession = getConnection().createSession(getClient().isSessTransacted(), ackMode); 104 } 105 return jmsSession; 106 } 107 108 public Destination [] createDestination(int destIndex, int destCount) throws JMSException { 109 110 if (getClient().isDestComposite()) { 111 return new Destination [] {createCompositeDestination(getClient().getDestName(), destIndex, destCount)}; 112 } else { 113 Destination [] dest = new Destination [destCount]; 114 for (int i=0; i<destCount; i++) { 115 dest[i] = createDestination(getClient().getDestName() + "." + (destIndex + i)); 116 } 117 118 return dest; 119 } 120 } 121 122 public Destination createCompositeDestination(int destIndex, int destCount) throws JMSException { 123 return createCompositeDestination(getClient().getDestName(), destIndex, destCount); 124 } 125 126 protected Destination createCompositeDestination(String name, int destIndex, int destCount) throws JMSException { 127 String compDestName; 128 String simpleName; 129 130 if (name.startsWith("queue://")) { 131 simpleName = name.substring("queue://".length()); 132 } else if (name.startsWith("topic://")) { 133 simpleName = name.substring("topic://".length()); 134 } else { 135 simpleName = name; 136 } 137 138 int i; 139 compDestName = name + "." + destIndex + ","; for (i=1; i<destCount-1; i++) { 141 compDestName += (simpleName + "." + (destIndex + i) +","); 142 } 143 compDestName += (simpleName + "." + (destIndex + i)); 145 return createDestination(compDestName); 146 } 147 148 protected Destination createDestination(String name) throws JMSException { 149 if (name.startsWith("queue://")) { 150 return getSession().createQueue(name.substring("queue://".length())); 151 } else if (name.startsWith("topic://")) { 152 return getSession().createTopic(name.substring("topic://".length())); 153 } else { 154 return getSession().createTopic(name); 155 } 156 } 157 158 } 159 | Popular Tags |