KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > opc > utils > JMSUtils


1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
25 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
27 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
28 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
29 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
30 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that Software is not designed, licensed or intended
34 * for use in the design, construction, operation or maintenance of
35 * any nuclear facility.
36 */

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 JavaDoc jmsDest, String JavaDoc property,
49               String JavaDoc value, String JavaDoc 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 JavaDoc 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 JavaDoc jmsDest, String JavaDoc property,
84               String JavaDoc value, Object JavaDoc 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 JavaDoc) 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 JavaDoc 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