View Javadoc

1   package org.yajul.jms;
2   
3   import javax.jms.*;
4   import java.util.Enumeration;
5   
6   /***
7    * Utility methods for JMS.
8    * <br>User: Joshua Davis
9    * Date: Sep 9, 2007
10   * Time: 10:41:52 AM
11   */
12  public class JmsHelper {
13      /***
14       * Clean up JMS producer objects.  Typically used in a finally block.
15       *
16       * @param sender  the sender
17       * @param session the session (may be null)
18       * @param conn    the connection (may be null)
19       */
20      public static void close(MessageProducer sender, Session session, Connection conn) {
21          if (sender != null) {
22              //noinspection EmptyCatchBlock
23              try {
24                  sender.close();
25              } catch (Exception ignore) {
26              }
27          }
28          if (session != null) {
29              //noinspection EmptyCatchBlock
30              try {
31                  session.close();
32              } catch (Exception ignore) {
33              }
34          }
35          if (conn != null) {
36              //noinspection EmptyCatchBlock
37              try {
38                  conn.close();
39              } catch (Exception ignore) {
40              }
41          }
42      }
43  
44      /***
45       * Clean up JMS consumer objects.    Typically used in a finally block.
46       *
47       * @param consumer the consumer
48       * @param session  the session (may be null)
49       * @param conn     the connection (may be null)
50       */
51      public static void close(MessageConsumer consumer, Session session, Connection conn) {
52          if (conn != null) {
53              //noinspection EmptyCatchBlock
54              try {
55                  conn.stop();
56              } catch (Exception ignore) {
57              }
58          }
59  
60          if (consumer != null) {
61              //noinspection EmptyCatchBlock
62              try {
63                  consumer.close();
64              } catch (Exception ignore) {
65              }
66          }
67          if (session != null) {
68              //noinspection EmptyCatchBlock
69              try {
70                  session.close();
71              } catch (Exception ignore) {
72              }
73          }
74          if (conn != null) {
75              //noinspection EmptyCatchBlock
76              try {
77                  conn.close();
78              } catch (Exception ignore) {
79              }
80          }
81      }
82  
83      /***
84       * Returns the object in the JMS message if it's an object message.
85       *
86       * @param message the JMS message
87       * @return the object in the JMS message if it's an object message, null otherwise.
88       */
89      public static Object getObject(Message message) {
90          if (message == null)
91              return null;
92          if (message instanceof ObjectMessage) {
93              ObjectMessage objectMessage = (ObjectMessage) message;
94              try {
95                  return objectMessage.getObject();
96              }
97              catch (JMSException e) {
98                  throw new RuntimeException(e);
99              }
100         } else
101             return null;
102     }
103 
104 
105     public static Long getNullableLongProperty(Message m, String property) throws JMSException {
106         return (Long) (m == null ? null : m.getObjectProperty(property));
107     }
108 
109     public static String getStringProperty(Message m, String property) throws JMSException {
110         return m == null ? null : m.getStringProperty(property);
111     }
112 
113     public static Message peek(Session session, Destination destination) throws JMSException {
114         QueueSession ses = (QueueSession) session;
115         Queue queue = (Queue) destination;
116         QueueBrowser browser = ses.createBrowser(queue);
117         Enumeration enumeration = browser.getEnumeration();
118         return enumeration.hasMoreElements() ? (Message) enumeration.nextElement() : null;
119     }
120 
121     public static boolean messagePropertyNullOrEqualTo(Message message, String propertyName, Long aLong) {
122         try {
123             boolean exists = message.propertyExists(propertyName);
124             if (exists) {
125                 long val = message.getLongProperty(propertyName);
126                 return val == aLong;
127             } else
128                 return true;
129         }
130         catch (JMSException e) {
131             throw new RuntimeException(e);
132         }
133     }
134 
135 }