1 55 56 package org.jboss.axis.transport.jms; 57 58 import org.jboss.axis.server.AxisServer; 59 import org.jboss.axis.utils.Messages; 60 import org.jboss.axis.utils.Options; 61 import org.jboss.logging.Logger; 62 63 import javax.jms.BytesMessage ; 64 import javax.jms.MessageListener ; 65 import java.io.BufferedInputStream ; 66 import java.io.FileInputStream ; 67 import java.io.IOException ; 68 import java.util.HashMap ; 69 import java.util.Properties ; 70 71 72 85 public class SimpleJMSListener implements MessageListener 86 { 87 protected static Logger log = Logger.getLogger(SimpleJMSListener.class); 88 89 private static boolean doThreads; 91 92 private JMSConnector connector; 93 private JMSEndpoint endpoint; 94 private AxisServer server; 95 96 public SimpleJMSListener(HashMap connectorMap, HashMap cfMap, 97 String destination, String username, 98 String password, boolean doThreads) 99 throws Exception 100 { 101 this.doThreads = doThreads; 102 103 try 104 { 105 connector = JMSConnectorFactory. 106 createServerConnector(connectorMap, 107 cfMap, 108 username, 109 password); 110 } 111 catch (Exception e) 112 { 113 log.error(Messages.getMessage("exception00"), e); 114 throw e; 115 } 116 117 endpoint = connector.createEndpoint(destination); 119 } 120 121 private static AxisServer myAxisServer = new AxisServer(); 123 124 protected static AxisServer getAxisServer() 125 { 126 return myAxisServer; 127 } 128 129 protected JMSConnector getConnector() 130 { 131 return connector; 132 } 133 134 138 public void onMessage(javax.jms.Message message) 139 { 140 try 141 { 142 SimpleJMSWorker worker = new SimpleJMSWorker(this, (BytesMessage )message); 144 145 147 if (doThreads) 149 { 150 Thread t = new Thread (worker); 151 t.start(); 152 } 153 else 154 { 155 worker.run(); 156 } 157 } 158 catch (ClassCastException cce) 159 { 160 log.error(Messages.getMessage("exception00"), cce); 161 cce.printStackTrace(); 162 return; 163 } 164 } 165 166 public void start() 167 throws Exception 168 { 169 endpoint.registerListener(this); 170 connector.start(); 171 } 172 173 public void shutdown() 174 throws Exception 175 { 176 endpoint.unregisterListener(this); 177 connector.stop(); 178 connector.shutdown(); 179 } 180 181 public static final HashMap createConnectorMap(Options options) 182 { 183 HashMap connectorMap = new HashMap (); 184 if (options.isFlagSet('t') > 0) 185 { 186 connectorMap.put(JMSConstants.DOMAIN, JMSConstants.DOMAIN_TOPIC); 188 } 189 return connectorMap; 190 } 191 192 public static final HashMap createCFMap(Options options) 193 throws IOException 194 { 195 String cfFile = options.isValueSet('c'); 196 if (cfFile == null) 197 return null; 198 199 Properties cfProps = new Properties (); 200 cfProps.load(new BufferedInputStream (new FileInputStream (cfFile))); 201 HashMap cfMap = new HashMap (cfProps); 202 return cfMap; 203 } 204 205 public static void main(String [] args) throws Exception 206 { 207 Options options = new Options(args); 208 209 if ((options.isFlagSet('?') > 0) || (options.isFlagSet('h') > 0)) 211 printUsage(); 212 213 SimpleJMSListener listener = new SimpleJMSListener(createConnectorMap(options), 214 createCFMap(options), 215 options.isValueSet('d'), 216 options.getUser(), 217 options.getPassword(), 218 options.isFlagSet('s') > 0); 219 } 220 221 public static void printUsage() 222 { 223 System.out.println("Usage: SimpleJMSListener [options]"); 224 System.out.println(" Opts: -? this message"); 225 System.out.println(); 226 System.out.println(" -c connection factory properties filename"); 227 System.out.println(" -d destination"); 228 System.out.println(" -t topic [absence of -t indicates queue]"); 229 System.out.println(); 230 System.out.println(" -u username"); 231 System.out.println(" -w password"); 232 System.out.println(); 233 System.out.println(" -s single-threaded listener"); 234 System.out.println(" [absence of option => multithreaded]"); 235 236 System.exit(1); 237 } 238 } 239 | Popular Tags |