1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.transport.jms;
18
19 import java.io.IOException;
20 import java.net.URI;
21 import javax.jms.BytesMessage;
22 import javax.jms.Connection;
23 import javax.jms.ConnectionFactory;
24 import javax.jms.Destination;
25 import javax.jms.JMSException;
26 import javax.jms.Message;
27 import javax.jms.MessageConsumer;
28 import javax.jms.MessageProducer;
29 import javax.jms.Queue;
30 import javax.jms.Session;
31 import javax.jms.TextMessage;
32 import javax.jms.Topic;
33
34 import org.springframework.jms.connection.ConnectionFactoryUtils;
35 import org.springframework.jms.core.MessagePostProcessor;
36 import org.springframework.jms.support.JmsUtils;
37 import org.springframework.jms.support.destination.JmsDestinationAccessor;
38 import org.springframework.util.StringUtils;
39 import org.springframework.ws.transport.WebServiceConnection;
40 import org.springframework.ws.transport.WebServiceMessageSender;
41 import org.springframework.ws.transport.jms.support.JmsTransportUtils;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public class JmsMessageSender extends JmsDestinationAccessor implements WebServiceMessageSender {
78
79
80 public static final long DEFAULT_RECEIVE_TIMEOUT = -1;
81
82
83 public static final String DEFAULT_TEXT_MESSAGE_ENCODING = "UTF-8";
84
85 private long receiveTimeout = DEFAULT_RECEIVE_TIMEOUT;
86
87 private String textMessageEncoding = DEFAULT_TEXT_MESSAGE_ENCODING;
88
89 private MessagePostProcessor postProcessor;
90
91
92
93
94
95
96
97
98
99
100 public JmsMessageSender() {
101 }
102
103
104
105
106
107
108 public JmsMessageSender(ConnectionFactory connectionFactory) {
109 setConnectionFactory(connectionFactory);
110 }
111
112
113
114
115
116
117 public void setReceiveTimeout(long receiveTimeout) {
118 this.receiveTimeout = receiveTimeout;
119 }
120
121
122 public void setTextMessageEncoding(String textMessageEncoding) {
123 this.textMessageEncoding = textMessageEncoding;
124 }
125
126
127
128
129
130 public void setPostProcessor(MessagePostProcessor postProcessor) {
131 this.postProcessor = postProcessor;
132 }
133
134 public WebServiceConnection createConnection(URI uri) throws IOException {
135 Connection jmsConnection = null;
136 Session jmsSession = null;
137 try {
138 jmsConnection = createConnection();
139 jmsSession = createSession(jmsConnection);
140 Destination requestDestination = resolveRequestDestination(jmsSession, uri);
141 Message requestMessage = createRequestMessage(jmsSession, uri);
142 JmsSenderConnection wsConnection =
143 new JmsSenderConnection(getConnectionFactory(), jmsConnection, jmsSession, requestDestination,
144 requestMessage);
145 wsConnection.setDeliveryMode(JmsTransportUtils.getDeliveryMode(uri));
146 wsConnection.setPriority(JmsTransportUtils.getPriority(uri));
147 wsConnection.setReceiveTimeout(receiveTimeout);
148 wsConnection.setResponseDestination(resolveResponseDestination(jmsSession, uri));
149 wsConnection.setTimeToLive(JmsTransportUtils.getTimeToLive(uri));
150 wsConnection.setTextMessageEncoding(textMessageEncoding);
151 wsConnection.setSessionTransacted(isSessionTransacted());
152 wsConnection.setPostProcessor(postProcessor);
153 return wsConnection;
154 }
155 catch (JMSException ex) {
156 JmsUtils.closeSession(jmsSession);
157 ConnectionFactoryUtils.releaseConnection(jmsConnection, getConnectionFactory(), true);
158 throw new JmsTransportException(ex);
159 }
160 }
161
162 public boolean supports(URI uri) {
163 return uri.getScheme().equals(JmsTransportConstants.JMS_URI_SCHEME);
164 }
165
166 private Destination resolveRequestDestination(Session session, URI uri) throws JMSException {
167 return resolveDestinationName(session, JmsTransportUtils.getDestinationName(uri));
168 }
169
170 private Destination resolveResponseDestination(Session session, URI uri) throws JMSException {
171 String destinationName = JmsTransportUtils.getReplyToName(uri);
172 return StringUtils.hasLength(destinationName) ? resolveDestinationName(session, destinationName) : null;
173 }
174
175 private Message createRequestMessage(Session session, URI uri) throws JMSException {
176 int messageType = JmsTransportUtils.getMessageType(uri);
177 if (messageType == JmsTransportConstants.BYTES_MESSAGE_TYPE) {
178 return session.createBytesMessage();
179 }
180 else if (messageType == JmsTransportConstants.TEXT_MESSAGE_TYPE) {
181 return session.createTextMessage();
182 }
183 else {
184 throw new IllegalArgumentException("Invalid message type [" + messageType + "].");
185 }
186
187 }
188
189
190 }