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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 public class JmsMessageSender extends JmsDestinationAccessor implements WebServiceMessageSender {
98
99
100 public static final long DEFAULT_RECEIVE_TIMEOUT = -1;
101
102
103 public static final String DEFAULT_TEXT_MESSAGE_ENCODING = "UTF-8";
104
105 private long receiveTimeout = DEFAULT_RECEIVE_TIMEOUT;
106
107 private String textMessageEncoding = DEFAULT_TEXT_MESSAGE_ENCODING;
108
109 private MessagePostProcessor postProcessor;
110
111
112
113
114
115
116
117
118
119
120 public JmsMessageSender() {
121 }
122
123
124
125
126
127
128 public JmsMessageSender(ConnectionFactory connectionFactory) {
129 setConnectionFactory(connectionFactory);
130 }
131
132
133
134
135
136
137 public void setReceiveTimeout(long receiveTimeout) {
138 this.receiveTimeout = receiveTimeout;
139 }
140
141
142 public void setTextMessageEncoding(String textMessageEncoding) {
143 this.textMessageEncoding = textMessageEncoding;
144 }
145
146
147
148
149
150 public void setPostProcessor(MessagePostProcessor postProcessor) {
151 this.postProcessor = postProcessor;
152 }
153
154 public WebServiceConnection createConnection(URI uri) throws IOException {
155 Connection jmsConnection = null;
156 Session jmsSession = null;
157 try {
158 jmsConnection = createConnection();
159 jmsSession = createSession(jmsConnection);
160 Destination requestDestination = resolveRequestDestination(jmsSession, uri);
161 Message requestMessage = createRequestMessage(jmsSession, uri);
162 JmsSenderConnection wsConnection =
163 new JmsSenderConnection(getConnectionFactory(), jmsConnection, jmsSession, requestDestination,
164 requestMessage);
165 wsConnection.setDeliveryMode(JmsTransportUtils.getDeliveryMode(uri));
166 wsConnection.setPriority(JmsTransportUtils.getPriority(uri));
167 wsConnection.setReceiveTimeout(receiveTimeout);
168 wsConnection.setResponseDestination(resolveResponseDestination(jmsSession, uri));
169 wsConnection.setTimeToLive(JmsTransportUtils.getTimeToLive(uri));
170 wsConnection.setTextMessageEncoding(textMessageEncoding);
171 wsConnection.setSessionTransacted(isSessionTransacted());
172 wsConnection.setPostProcessor(postProcessor);
173 return wsConnection;
174 }
175 catch (JMSException ex) {
176 JmsUtils.closeSession(jmsSession);
177 ConnectionFactoryUtils.releaseConnection(jmsConnection, getConnectionFactory(), true);
178 throw new JmsTransportException(ex);
179 }
180 }
181
182 public boolean supports(URI uri) {
183 return uri.getScheme().equals(JmsTransportConstants.JMS_URI_SCHEME);
184 }
185
186 private Destination resolveRequestDestination(Session session, URI uri) throws JMSException {
187 return resolveDestinationName(session, JmsTransportUtils.getDestinationName(uri));
188 }
189
190 private Destination resolveResponseDestination(Session session, URI uri) throws JMSException {
191 String destinationName = JmsTransportUtils.getReplyToName(uri);
192 return StringUtils.hasLength(destinationName) ? resolveDestinationName(session, destinationName) : null;
193 }
194
195 private Message createRequestMessage(Session session, URI uri) throws JMSException {
196 int messageType = JmsTransportUtils.getMessageType(uri);
197 if (messageType == JmsTransportConstants.BYTES_MESSAGE_TYPE) {
198 return session.createBytesMessage();
199 }
200 else if (messageType == JmsTransportConstants.TEXT_MESSAGE_TYPE) {
201 return session.createTextMessage();
202 }
203 else {
204 throw new IllegalArgumentException("Invalid message type [" + messageType + "].");
205 }
206
207 }
208
209
210 }