View Javadoc

1   /*
2    * $Id: ActiveMqJmsConnector.java 10414 2008-01-21 10:14:57Z akuzmin $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.jms.activemq;
12  
13  import org.mule.providers.ConnectException;
14  import org.mule.providers.jms.JmsConnector;
15  import org.mule.providers.jms.xa.ConnectionInvocationHandler;
16  
17  import java.lang.reflect.Method;
18  import java.lang.reflect.Proxy;
19  
20  import javax.jms.Connection;
21  
22  /**
23   * ActiveMQ 4.x-specific JMS connector.
24   */
25  public class ActiveMqJmsConnector extends JmsConnector
26  {
27      /**
28       * Constructs a new ActiveMqJmsConnector.
29       */
30      public ActiveMqJmsConnector()
31      {
32          setEagerConsumer(false);
33          // TODO MULE-1409 better support for ActiveMQ 4.x temp destinations
34      }
35  
36      protected void applyVendorSpecificConnectionFactoryProperties()
37      {
38          super.applyVendorSpecificConnectionFactoryProperties();
39  
40          try
41          {
42              Method getRedeliveryPolicyMethod = getConnectionFactory().getClass().getMethod("getRedeliveryPolicy", new Class[]{});
43              Object redeliveryPolicy = getRedeliveryPolicyMethod.invoke(getConnectionFactory(), new Object[]{});
44              Method setMaximumRedeliveriesMethod = redeliveryPolicy.getClass().getMethod("setMaximumRedeliveries", new Class[]{Integer.TYPE});
45              setMaximumRedeliveriesMethod.invoke(redeliveryPolicy, new Object[]{new Integer(getMaxRedelivery())});
46          }
47          catch (Exception e)
48          {
49              logger.error("Can not set MaxRedelivery parameter to RedeliveryPolicy " + e);
50          }
51      }
52  
53      /**
54       * Will additionally try to cleanup the ActiveMq connection, otherwise there's a deadlock on shutdown.
55       */
56      protected void doDisconnect() throws ConnectException
57      {
58          try
59          {
60              Connection connection = getConnection();
61              if (connection == null)
62              {
63                  return;
64              }
65  
66              final Class clazz = connection.getClass();
67              Method cleanupMethod;
68              if (Proxy.isProxyClass(clazz))
69              {
70                  ConnectionInvocationHandler handler =
71                          (ConnectionInvocationHandler) Proxy.getInvocationHandler(connection);
72                  // this is really an XA connection, bypass the java.lang.reflect.Proxy as it
73                  // can't delegate to non-interfaced methods (like proprietary 'cleanup' one)
74                  // TODO check if CGlib will manage to enhance the AMQ connection class,
75                  // there are no final methods, but a number of private ones, though
76                  connection = (Connection) handler.getTargetObject();
77                  Class realConnectionClass = connection.getClass();
78                  cleanupMethod = realConnectionClass.getMethod("cleanup", null);
79              }
80              else
81              {
82                  cleanupMethod = clazz.getMethod("cleanup", null);
83              }
84  
85              try
86              {
87                  if (cleanupMethod != null)
88                  {
89                      cleanupMethod.invoke(connection, null);
90                  }
91              }
92              finally
93              {
94                  connection.close();
95              }
96          }
97          catch (Exception e)
98          {
99              throw new ConnectException(e, this);
100         }
101         finally
102         {
103             setConnection(null);
104         }
105     }
106 }