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 javax.ejb.EJBException;
20 import javax.ejb.MessageDrivenBean;
21 import javax.jms.Connection;
22 import javax.jms.ConnectionFactory;
23 import javax.jms.JMSException;
24 import javax.jms.Message;
25 import javax.jms.Session;
26 import javax.naming.NamingException;
27
28 import org.springframework.ejb.support.AbstractJmsMessageDrivenBean;
29 import org.springframework.jms.connection.ConnectionFactoryUtils;
30 import org.springframework.jms.core.MessagePostProcessor;
31 import org.springframework.jms.support.JmsUtils;
32 import org.springframework.jndi.JndiLookupFailureException;
33 import org.springframework.ws.WebServiceMessageFactory;
34 import org.springframework.ws.transport.WebServiceMessageReceiver;
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class WebServiceMessageDrivenBean extends AbstractJmsMessageDrivenBean {
49
50
51 public static final String CONNECTION_FACTORY_BEAN_NAME = "connectionFactory";
52
53
54 public static final String MESSAGE_FACTORY_BEAN_NAME = "messageFactory";
55
56
57 public static final String MESSAGE_RECEIVER_BEAN_NAME = "messageReceiver";
58
59 private JmsMessageReceiver delegate;
60
61 private ConnectionFactory connectionFactory;
62
63
64 public void onMessage(Message message) {
65 Connection connection = null;
66 Session session = null;
67 try {
68 connection = createConnection(connectionFactory);
69 session = createSession(connection);
70 delegate.handleMessage(message, session);
71 }
72 catch (JmsTransportException ex) {
73 throw JmsUtils.convertJmsAccessException(ex.getJmsException());
74 }
75 catch (JMSException ex) {
76 throw JmsUtils.convertJmsAccessException(ex);
77 }
78 catch (Exception ex) {
79 throw new EJBException(ex);
80 }
81 finally {
82 JmsUtils.closeSession(session);
83 ConnectionFactoryUtils.releaseConnection(connection, connectionFactory, true);
84 }
85 }
86
87
88
89
90
91
92
93
94 @Override
95 protected void onEjbCreate() {
96 try {
97 connectionFactory = createConnectionFactory();
98 delegate = new JmsMessageReceiver();
99 delegate.setMessageFactory(createMessageFactory());
100 delegate.setMessageReceiver(createMessageReceiver());
101 delegate.setPostProcessor(createPostProcessor());
102 }
103 catch (NamingException ex) {
104 throw new JndiLookupFailureException("Could not create connection", ex);
105 }
106 catch (JMSException ex) {
107 throw JmsUtils.convertJmsAccessException(ex);
108 }
109 catch (Exception ex) {
110 throw new EJBException(ex);
111 }
112 }
113
114
115 protected ConnectionFactory createConnectionFactory() throws Exception {
116 return (ConnectionFactory) getBeanFactory().getBean(CONNECTION_FACTORY_BEAN_NAME, ConnectionFactory.class);
117 }
118
119
120 protected WebServiceMessageFactory createMessageFactory() {
121 return (WebServiceMessageFactory) getBeanFactory()
122 .getBean(MESSAGE_FACTORY_BEAN_NAME, WebServiceMessageFactory.class);
123 }
124
125
126 protected WebServiceMessageReceiver createMessageReceiver() {
127 return (WebServiceMessageReceiver) getBeanFactory()
128 .getBean(MESSAGE_RECEIVER_BEAN_NAME, WebServiceMessageReceiver.class);
129 }
130
131
132
133
134
135
136
137
138
139
140
141 protected Connection createConnection(ConnectionFactory connectionFactory) throws JMSException {
142 return connectionFactory.createConnection();
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156 protected Session createSession(Connection connection) throws JMSException {
157 return connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
158 }
159
160
161
162
163
164
165
166 protected MessagePostProcessor createPostProcessor() {
167 return null;
168 }
169
170 }