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