1 29 30 package com.caucho.jms.resource; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.jms.AbstractDestination; 34 import com.caucho.jms.ConnectionFactoryImpl; 35 import com.caucho.services.message.MessageSender; 36 import com.caucho.services.message.MessageServiceException; 37 import com.caucho.util.L10N; 38 import com.caucho.util.Log; 39 40 import javax.annotation.*; 41 import javax.jms.*; 42 import java.util.HashMap ; 43 import java.util.logging.Logger ; 44 45 48 public class MessageSenderResource implements MessageSender { 49 private static final L10N L = new L10N(MessageSenderResource.class); 50 private static final Logger log = Log.open(MessageSenderResource.class); 51 52 private ConnectionFactory _connFactory; 53 private Connection _conn; 54 private Destination _destination; 55 56 61 public void setConnectionFactory(ConnectionFactory factory) 62 { 63 _connFactory = factory; 64 } 65 66 71 public void setDestination(Destination destination) 72 { 73 _destination = destination; 74 } 75 76 82 @PostConstruct 83 public void init() throws JMSException, ConfigException 84 { 85 if (_destination == null) 86 throw new ConfigException(L.l("'destination' required for message sender.")); 87 88 if (_connFactory == null && _destination instanceof AbstractDestination) 89 _connFactory = new ConnectionFactoryImpl(); 90 91 if (_connFactory == null) 92 throw new ConfigException(L.l("'connection-factory' required for message sender")); 93 94 _conn = _connFactory.createConnection(); 95 } 96 97 104 public void send(HashMap header, Object value) 105 throws MessageServiceException 106 { 107 try { 108 Session session = getSession(); 109 110 try { 111 Message message; 112 113 if (value == null) { 114 message = session.createMessage(); 115 } 116 else if (value instanceof String ) { 117 message = session.createTextMessage((String ) value); 118 } 119 else if (value instanceof java.io.Serializable ) { 120 ObjectMessage objMessage = session.createObjectMessage(); 121 objMessage.setObject((java.io.Serializable ) value); 122 message = objMessage; 123 } 124 else { 125 throw new MessageServiceException(L.l("value '{0}' must be serializable", 126 value)); 127 } 128 129 MessageProducer producer = session.createProducer(_destination); 130 131 producer.send(message); 132 133 producer.close(); 134 } finally { 135 session.close(); 136 } 137 } catch (MessageServiceException e) { 138 throw e; 139 } catch (Exception e) { 140 throw new MessageServiceException(e); 141 } 142 } 143 144 147 private Session getSession() throws JMSException 148 { 149 return _conn.createSession(false, Session.AUTO_ACKNOWLEDGE); 150 } 151 } 152 153 | Popular Tags |