summaryrefslogtreecommitdiff
path: root/java/client/src/main/java/org/apache/qpid/client/AMQConnectionDelegate_0_10.java
blob: e741d4071cd08a38520f0022310bbed6e4652cf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package org.apache.qpid.client;

import java.io.IOException;

import javax.jms.JMSException;
import javax.jms.XASession;

import org.apache.qpid.AMQException;
import org.apache.qpid.AMQProtocolException;
import org.apache.qpid.protocol.AMQConstant;
import org.apache.qpid.client.failover.FailoverException;
import org.apache.qpid.jms.BrokerDetails;
import org.apache.qpid.jms.Session;
import org.apache.qpidity.nclient.Client;
import org.apache.qpidity.nclient.ClosedListener;
import org.apache.qpidity.ErrorCode;
import org.apache.qpidity.QpidException;
import org.apache.qpidity.ProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AMQConnectionDelegate_0_10 implements AMQConnectionDelegate, ClosedListener
{
    /**
     * This class logger.
     */
    private static final Logger _logger = LoggerFactory.getLogger(AMQConnectionDelegate_0_10.class);

    /**
     * The AMQ Connection.
     */
    private AMQConnection _conn;

    /**
     * The QpidConeection instance that is mapped with thie JMS connection.
     */
    org.apache.qpidity.nclient.Connection _qpidConnection;

    //--- constructor
    public AMQConnectionDelegate_0_10(AMQConnection conn)
    {
        _conn = conn;
    }

    /**
     * create a Session and start it if required.
     */
    public Session createSession(boolean transacted, int acknowledgeMode, int prefetchHigh, int prefetchLow)
            throws JMSException
    {
        _conn.checkNotClosed();
        int channelId = _conn._idFactory.incrementAndGet();
        AMQSession session;
        try
        {
            session = new AMQSession_0_10(_qpidConnection, _conn, channelId, transacted, acknowledgeMode, prefetchHigh,
                                          prefetchLow);
            _conn.registerSession(channelId, session);
            if (_conn._started)
            {
                session.start();
            }
        }
        catch (Exception e)
        {
            throw new JMSAMQException("cannot create session", e);
        }
        return session;
    }

    /**
     * create an XA Session and start it if required.
     */
    public XASession createXASession(int prefetchHigh, int prefetchLow) throws JMSException
    {
        _conn.checkNotClosed();
        int channelId = _conn._idFactory.incrementAndGet();
        XASessionImpl session;
        try
        {
            session = new XASessionImpl(_qpidConnection, _conn, channelId, prefetchHigh, prefetchLow);
            _conn.registerSession(channelId, session);
            if (_conn._started)
            {
                session.start();
            }
        }
        catch (Exception e)
        {
            throw new JMSAMQException("cannot create session", e);
        }
        return session;
    }


    /**
     * Make a connection with the broker
     *
     * @param brokerDetail The detail of the broker to connect to.
     * @throws IOException
     * @throws AMQException
     */
    public void makeBrokerConnection(BrokerDetails brokerDetail) throws IOException, AMQException
    {
        _qpidConnection = Client.createConnection();
        try
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("creating connection with broker " + " host: " + brokerDetail
                        .getHost() + " port: " + brokerDetail.getPort() + " virtualhost: " + _conn
                        .getVirtualHost() + "user name: " + _conn.getUsername() + "password: " + _conn.getPassword());
            }
            _qpidConnection.connect(brokerDetail.getHost(), brokerDetail.getPort(), _conn.getVirtualHost(),
                                    _conn.getUsername(), _conn.getPassword());
            _qpidConnection.setClosedListener(this);
        }
        catch(ProtocolException pe)
        {
           throw new AMQProtocolException(null, pe.getMessage(), pe);
        }
        catch (QpidException e)
        {
            throw new AMQException(AMQConstant.CHANNEL_ERROR, "cannot connect to broker", e);
        }
    }

    /**
     * Not supported at this level.
     */
    public void resubscribeSessions() throws JMSException, AMQException, FailoverException
    {
        //NOT implemented as railover is handled at a lower level
        throw new FailoverException("failing to reconnect during failover, operation not supported.");
    }


    public void closeConneciton(long timeout) throws JMSException, AMQException
    {
        try
        {
            _qpidConnection.close();
        }
        catch (QpidException e)
        {
            throw new AMQException(AMQConstant.CHANNEL_ERROR, "cannot close connection", e);
        }

    }

    public void onClosed(ErrorCode errorCode, String reason, Throwable t)
    {
        if (_logger.isDebugEnabled())
        {
            _logger.debug("Received a connection close from the broker: Error code : " + errorCode.getCode(), t);
        }
        if (_conn._exceptionListener != null)
        {
            JMSException ex = new JMSException(reason,String.valueOf(errorCode.getCode()));
            if (t != null)
            {
                ex.initCause(t);
            }

            _conn._exceptionListener.onException(ex);
        }
    }
}