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