1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.jms.activemq; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.transport.ConnectException; |
15 | |
import org.mule.transport.jms.JmsConnector; |
16 | |
import org.mule.transport.jms.xa.TargetInvocationHandler; |
17 | |
import org.mule.util.ClassUtils; |
18 | |
|
19 | |
import java.lang.reflect.InvocationTargetException; |
20 | |
import java.lang.reflect.Method; |
21 | |
import java.lang.reflect.Proxy; |
22 | |
|
23 | |
import javax.jms.Connection; |
24 | |
import javax.jms.ConnectionFactory; |
25 | |
import javax.jms.JMSException; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class ActiveMQJmsConnector extends JmsConnector |
31 | |
{ |
32 | |
public static final String ACTIVEMQ_CONNECTION_FACTORY_CLASS = "org.apache.activemq.ActiveMQConnectionFactory"; |
33 | |
public static final String DEFAULT_BROKER_URL = "vm://localhost?broker.persistent=false&broker.useJmx=false"; |
34 | |
|
35 | 0 | private String brokerURL = DEFAULT_BROKER_URL; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public ActiveMQJmsConnector(MuleContext context) |
41 | |
{ |
42 | 0 | super(context); |
43 | 0 | setEagerConsumer(false); |
44 | |
|
45 | 0 | } |
46 | |
|
47 | |
protected ConnectionFactory getDefaultConnectionFactory() throws Exception |
48 | |
{ |
49 | 0 | ConnectionFactory connectionFactory = (ConnectionFactory) |
50 | |
ClassUtils.instanciateClass(ACTIVEMQ_CONNECTION_FACTORY_CLASS, getBrokerURL()); |
51 | 0 | applyVendorSpecificConnectionFactoryProperties(connectionFactory); |
52 | 0 | return connectionFactory; |
53 | |
} |
54 | |
|
55 | |
protected void applyVendorSpecificConnectionFactoryProperties(ConnectionFactory connectionFactory) |
56 | |
{ |
57 | |
try |
58 | |
{ |
59 | 0 | Method getRedeliveryPolicyMethod = connectionFactory.getClass().getMethod("getRedeliveryPolicy"); |
60 | 0 | Object redeliveryPolicy = getRedeliveryPolicyMethod.invoke(connectionFactory); |
61 | 0 | Method setMaximumRedeliveriesMethod = redeliveryPolicy.getClass().getMethod("setMaximumRedeliveries", Integer.TYPE); |
62 | |
|
63 | 0 | setMaximumRedeliveriesMethod.invoke(redeliveryPolicy, getMaxRedelivery() + 1); |
64 | |
} |
65 | 0 | catch (Exception e) |
66 | |
{ |
67 | 0 | logger.error("Can not set MaxRedelivery parameter to RedeliveryPolicy " + e); |
68 | 0 | } |
69 | 0 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
protected void doDisconnect() throws ConnectException |
75 | |
{ |
76 | |
try |
77 | |
{ |
78 | 0 | Connection connection = getConnection(); |
79 | 0 | if (connection == null) |
80 | |
{ |
81 | |
return; |
82 | |
} |
83 | |
|
84 | 0 | final Class clazz = connection.getClass(); |
85 | |
Method cleanupMethod; |
86 | 0 | if (Proxy.isProxyClass(clazz)) |
87 | |
{ |
88 | 0 | TargetInvocationHandler handler = |
89 | |
(TargetInvocationHandler) Proxy.getInvocationHandler(connection); |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | 0 | connection = (Connection) handler.getTargetObject(); |
95 | 0 | Class realConnectionClass = connection.getClass(); |
96 | 0 | cleanupMethod = realConnectionClass.getMethod("cleanup", (Class[])null); |
97 | 0 | } |
98 | |
else |
99 | |
{ |
100 | 0 | cleanupMethod = clazz.getMethod("cleanup", (Class[])null); |
101 | |
} |
102 | |
|
103 | |
try |
104 | |
{ |
105 | 0 | if (cleanupMethod != null) |
106 | |
{ |
107 | 0 | cleanupMethod.invoke(connection, (Object[])null); |
108 | |
} |
109 | |
} |
110 | 0 | catch (InvocationTargetException ex) |
111 | |
{ |
112 | 0 | logger.warn("Exception cleaning up JMS connection", ex); |
113 | |
} |
114 | |
finally |
115 | |
{ |
116 | 0 | try |
117 | |
{ |
118 | 0 | connection.close(); |
119 | |
} |
120 | 0 | catch (JMSException ex) |
121 | |
{ |
122 | 0 | logger.warn("Exception closing JMS connection", ex); |
123 | 0 | } |
124 | 0 | } |
125 | |
} |
126 | 0 | catch (Exception e) |
127 | |
{ |
128 | 0 | throw new ConnectException(e, this); |
129 | |
} |
130 | |
finally |
131 | |
{ |
132 | |
|
133 | 0 | setConnection(null); |
134 | 0 | } |
135 | 0 | } |
136 | |
|
137 | |
public String getBrokerURL() |
138 | |
{ |
139 | 0 | return brokerURL; |
140 | |
} |
141 | |
|
142 | |
public void setBrokerURL(String brokerURL) |
143 | |
{ |
144 | 0 | this.brokerURL = brokerURL; |
145 | 0 | } |
146 | |
} |