summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/main/java/org/apache/qpid/test/unit/ack/QuickAcking.java
blob: 6c83136511decac7e802c9b345370b690237a8d4 (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
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.test.unit.ack;

import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
import org.apache.qpid.client.AMQConnection;
import org.apache.qpid.jms.ConnectionListener;
import org.apache.qpid.test.utils.QpidBrokerTestCase;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;

/**
 * This is a quick manual test to validate acking after failover with a
 * transacted session.
 *
 * Start an external broker then run this test. Std Err will print.
 * Sent Message: 1
 * Received Message: 1
 *
 * You can then restart the external broker, which will cause failover, which
 * will be complete when the following appears.
 *
 * Failover Complete
 *
 * A second message send/receive cycle is then done to validate that the
 * connection/session are still working.
 *
 */
public class QuickAcking extends QpidBrokerTestCase implements ConnectionListener
{
    protected AMQConnection _connection;
    protected Queue _queue;
    protected Session _session;
    protected MessageConsumer _consumer;
    private CountDownLatch _failedOver;
    private static final String INDEX = "INDEX";
    private int _count = 0;

    public void setUp()
    {
        // Prevent broker startup. Broker must be run manually.
    }

    public void test() throws Exception
    {
        _failedOver = new CountDownLatch(1);

        _connection = new AMQConnection("amqp://guest:guest@client/test?brokerlist='localhost?retries='20'&connectdelay='2000''");

        _session = _connection.createSession(true, Session.SESSION_TRANSACTED);
        _queue = _session.createQueue("QAtest");
        _consumer = _session.createConsumer(_queue);
        _connection.setConnectionListener(this);
        _connection.start();

        sendAndReceive();

        _failedOver.await();

        sendAndReceive();

    }

    private void sendAndReceive()
            throws Exception
    {
        sendMessage();

        Message message = _consumer.receive();

        if (message.getIntProperty(INDEX) != _count)
        {
            throw new Exception("Incorrect message recieved:" + _count);
        }

        if (_session.getTransacted())
        {
            _session.commit();
        }
        System.err.println("Recevied Message:" + _count);
    }

    private void sendMessage() throws JMSException
    {
        MessageProducer producer = _session.createProducer(_queue);
        Message message = _session.createMessage();
        _count++;
        message.setIntProperty(INDEX, _count);

        producer.send(message);
        if (_session.getTransacted())
        {
            _session.commit();
        }
        producer.close();

        System.err.println("Sent Message:" + _count);
    }

    public void bytesSent(long count)
    {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void bytesReceived(long count)
    {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public boolean preFailover(boolean redirect)
    {
        return true;
    }

    public boolean preResubscribe()
    {
        return true;
    }

    public void failoverComplete()
    {
        System.err.println("Failover Complete");
        _failedOver.countDown();
    }
}