1 37 38 package com.sun.j2ee.blueprints.opc.utils; 39 40 import javax.jms.*; 41 42 import com.sun.j2ee.blueprints.servicelocator.*; 43 import com.sun.j2ee.blueprints.servicelocator.ejb.*; 44 import com.sun.j2ee.blueprints.opc.JNDINames; 45 46 public class JMSUtils { 47 48 public static boolean sendMessage(String jmsDest, String property, 49 String value, String xmlDoc) { 50 Connection jmsCon = null; 51 try { 52 ServiceLocator sl = new ServiceLocator(); 53 ConnectionFactory jmsConFactory = sl.getJMSConnectionFactory(JNDINames.OPC_QUEUE_CONNECTION_FACTORY); 54 Destination target = sl.getJMSDestination(jmsDest); 55 jmsCon = jmsConFactory.createConnection(); 56 Session jmsSession = jmsCon.createSession(true,0); 57 MessageProducer jmsSender = jmsSession.createProducer(target); 58 TextMessage message = jmsSession.createTextMessage(xmlDoc); 59 message.setStringProperty(property, value); 60 jmsSender.send(message); 61 } catch (ServiceLocatorException se) { 62 System.err.println("JMSUtil exception " + se.getMessage()); 63 return false; 64 } catch (JMSException exe) { 65 System.err.println("JMSUtil exception " + exe.getMessage()); 66 return false; 67 } catch (Exception ge) { 68 System.err.println("JMSUtil exception " + ge.getMessage()); 69 return false; 70 } finally { 71 if (jmsCon != null) { 72 try { 73 jmsCon.close(); 74 } catch (JMSException exe) { 75 System.err.println("JMSUtil exception " + exe.getMessage()); 76 return false; 77 } 78 } 79 } 80 return true; 81 } 82 83 public static boolean sendMessage(String jmsDest, String property, 84 String value, Object obj) { 85 Connection jmsCon = null; 86 try { 87 ServiceLocator sl = new ServiceLocator(); 88 ConnectionFactory jmsConFactory = sl.getJMSConnectionFactory(JNDINames.OPC_QUEUE_CONNECTION_FACTORY); 89 Destination target = sl.getJMSDestination(jmsDest); 90 jmsCon = jmsConFactory.createConnection(); 91 Session jmsSession = jmsCon.createSession(true,0); 92 MessageProducer jmsSender = jmsSession.createProducer(target); 93 ObjectMessage message = jmsSession.createObjectMessage((java.io.Serializable ) obj); 94 message.setStringProperty(property, value); 95 jmsSender.send(message); 96 } catch (ServiceLocatorException se) { 97 System.err.println("JMSUtil exception" + se.getMessage()); 98 return false; 99 } catch (JMSException exe) { 100 System.err.println("JMSUtil exception " + exe.getMessage()); 101 return false; 102 } catch (Exception ge) { 103 System.err.println("JMSUtil exception " + ge.getMessage()); 104 return false; 105 } finally { 106 if (jmsCon != null) { 107 try { 108 jmsCon.close(); 109 } catch (JMSException exe) { 110 System.err.println("JMSUtil exception " + exe.getMessage()); 111 return false; 112 } 113 } 114 } 115 return true; 116 } 117 } 118 | Popular Tags |