1 16 17 package org.apache.axis.transport.jms; 18 19 import org.apache.axis.components.jms.JMSVendorAdapter; 20 import org.apache.axis.components.jms.JMSVendorAdapterFactory; 21 import org.apache.axis.components.logger.LogFactory; 22 import org.apache.axis.server.AxisServer; 23 import org.apache.axis.utils.Messages; 24 import org.apache.axis.utils.Options; 25 import org.apache.commons.logging.Log; 26 27 import javax.jms.BytesMessage ; 28 import javax.jms.MessageListener ; 29 import java.io.BufferedInputStream ; 30 import java.io.FileInputStream ; 31 import java.io.IOException ; 32 import java.util.HashMap ; 33 import java.util.Properties ; 34 35 36 49 public class SimpleJMSListener implements MessageListener 50 { 51 protected static Log log = 52 LogFactory.getLog(SimpleJMSListener.class.getName()); 53 54 private static boolean doThreads; 56 57 private JMSConnector connector; 58 private JMSEndpoint endpoint; 59 private AxisServer server; 60 private HashMap connectorProps; 61 62 public SimpleJMSListener(HashMap connectorMap, HashMap cfMap, 63 String destination, String username, 64 String password, boolean doThreads) 65 throws Exception 66 { 67 SimpleJMSListener.doThreads = doThreads; 68 69 try { 70 JMSVendorAdapter adapter = JMSVendorAdapterFactory.getJMSVendorAdapter(); 72 connector = JMSConnectorFactory.createServerConnector(connectorMap, 73 cfMap, 74 username, 75 password, 76 adapter); 77 connectorProps = connectorMap; 78 } catch (Exception e) { 79 log.error(Messages.getMessage("exception00"), e); 80 throw e; 81 } 82 83 endpoint = connector.createEndpoint(destination); 85 } 86 87 private static AxisServer myAxisServer = new AxisServer(); 89 90 protected static AxisServer getAxisServer() 91 { 92 return myAxisServer; 93 } 94 95 protected JMSConnector getConnector() 96 { 97 return connector; 98 } 99 100 104 public void onMessage(javax.jms.Message message) 105 { 106 try 107 { 108 SimpleJMSWorker worker = new SimpleJMSWorker(this, (BytesMessage )message); 110 111 if (doThreads) { 113 Thread t = new Thread (worker); 114 t.start(); 115 } else { 116 worker.run(); 117 } 118 } 119 catch(ClassCastException cce) 120 { 121 log.error(Messages.getMessage("exception00"), cce); 122 cce.printStackTrace(); 123 return; 124 } 125 } 126 127 public void start() 128 throws Exception 129 { 130 endpoint.registerListener(this, connectorProps); 131 connector.start(); 132 } 133 134 public void shutdown() 135 throws Exception 136 { 137 endpoint.unregisterListener(this); 138 connector.stop(); 139 connector.shutdown(); 140 } 141 142 public static final HashMap createConnectorMap(Options options) 143 { 144 HashMap connectorMap = new HashMap (); 145 if (options.isFlagSet('t') > 0) 146 { 147 connectorMap.put(JMSConstants.DOMAIN, JMSConstants.DOMAIN_TOPIC); 149 } 150 return connectorMap; 151 } 152 153 public static final HashMap createCFMap(Options options) 154 throws IOException 155 { 156 String cfFile = options.isValueSet('c'); 157 if(cfFile == null) 158 return null; 159 160 Properties cfProps = new Properties (); 161 cfProps.load(new BufferedInputStream (new FileInputStream (cfFile))); 162 HashMap cfMap = new HashMap (cfProps); 163 return cfMap; 164 } 165 166 public static void main(String [] args) throws Exception 167 { 168 Options options = new Options(args); 169 170 if ((options.isFlagSet('?') > 0) || (options.isFlagSet('h') > 0)) 172 printUsage(); 173 174 SimpleJMSListener listener = new SimpleJMSListener(createConnectorMap(options), 175 createCFMap(options), 176 options.isValueSet('d'), 177 options.getUser(), 178 options.getPassword(), 179 options.isFlagSet('s') > 0); 180 listener.start(); 181 } 182 183 public static void printUsage() 184 { 185 System.out.println("Usage: SimpleJMSListener [options]"); 186 System.out.println(" Opts: -? this message"); 187 System.out.println(); 188 System.out.println(" -c connection factory properties filename"); 189 System.out.println(" -d destination"); 190 System.out.println(" -t topic [absence of -t indicates queue]"); 191 System.out.println(); 192 System.out.println(" -u username"); 193 System.out.println(" -w password"); 194 System.out.println(); 195 System.out.println(" -s single-threaded listener"); 196 System.out.println(" [absence of option => multithreaded]"); 197 198 System.exit(1); 199 } 200 } 201 | Popular Tags |