Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. package th.co.tac.svcbiz.util;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Hashtable;
  5. import java.util.Properties;
  6.  
  7. import javax.jms.ObjectMessage;
  8. import javax.jms.Queue;
  9. import javax.jms.QueueConnection;
  10. import javax.jms.QueueConnectionFactory;
  11. import javax.jms.QueueSender;
  12. import javax.jms.QueueSession;
  13. import javax.naming.Context;
  14. import javax.naming.InitialContext;
  15.  
  16. import org.apache.log4j.Logger;
  17.  
  18. import th.co.tac.ws.ussd.log.vo.USSDLogVO;
  19.  
  20. /**
  21. * <p>Cluster</p>
  22. * @version 1.3
  23. * @author Phongsathorn Anguyarn <phongsathorn@xp-link.com>
  24. * @since December, 2014
  25. *
  26. */
  27. public class JMSUtil implements Serializable {
  28.  
  29. private static final long serialVersionUID = 6593240270204882766L;
  30.  
  31. public static void sendMessageToMDB(String systemName, String qConn, String qName, Properties message, USSDLogVO ussdLog, int priority, Logger callerLog) throws Exception {
  32. callerLog.info("Sending JMS message to Queue=" + qName);
  33. Hashtable<String, Object> params = new Hashtable<String, Object>();
  34. params.put("reqMsg", message);
  35. params.put("logVo", ussdLog);
  36. QueueConnection conn = null;
  37. QueueSession session = null;
  38. QueueSender sender = null;
  39. long start = System.currentTimeMillis();
  40. try {
  41. Context ctx = new InitialContext();
  42. QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup(qConn);
  43. Queue queue = (Queue) ctx.lookup(qName);
  44. conn = factory.createQueueConnection();
  45. session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
  46. sender = session.createSender(queue);
  47. ObjectMessage jmsMessage = session.createObjectMessage();
  48. jmsMessage.setObject(params);
  49. jmsMessage.setJMSPriority(priority);
  50. callerLog.info("Priority=" + priority + ", SEND_MSG=" + jmsMessage);
  51. sender.send(jmsMessage);
  52. } catch (Exception e) {
  53. callerLog.error("Error while sending request to JMS Queue.");
  54. throw e;
  55. } finally {
  56. if (sender != null) {
  57. try {
  58. sender.close();
  59. } catch (Exception ex) {
  60. ex.printStackTrace();
  61. }
  62. }
  63. if (session != null) {
  64. try {
  65. session.close();
  66. } catch (Exception ex) {
  67. ex.printStackTrace();
  68. }
  69. }
  70. if (conn != null) {
  71. try {
  72. conn.close();
  73. } catch (Exception ex) {
  74. ex.printStackTrace();
  75. }
  76. }
  77. long end = System.currentTimeMillis() - start;
  78. callerLog.info("SERVICE_NAME:" + systemName + "|SUCCESS workingTime = " + end);
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement