summaryrefslogtreecommitdiff
path: root/qpid/java/perftests/src/main/java/org/apache/qpid/perftests/dlq/client/Client.java
blob: 81517b6fbc3380d998c510b9df4d1b75eda17035 (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
package org.apache.qpid.perftests.dlq.client;

import static org.apache.qpid.perftests.dlq.client.Config.*;

import java.util.Properties;
import java.util.concurrent.Callable;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Session;

import org.apache.qpid.client.AMQConnection;
import org.apache.qpid.client.configuration.ClientProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Parent abstract class for all performance test clients that connect to a
 * broker and perform test operations. All clients are {@link Callable}
 * objects that return an integer value, or throw an exception. The
 * {@link #connect()} method returns a boolean to indicate whether the
 * broker connection succeeded, and can be used to abort tests if there is
 * no available broker.
 */
public abstract class Client implements Callable<Integer>
{
    protected static final Logger _log = LoggerFactory.getLogger(Client.class);
    
    protected Properties _props;
    
    protected String _broker;
    protected int _maxRedelivery;
    protected int _maxPrefetch;
    protected int _sessionType;
    protected boolean _transacted;
    protected boolean _clientAck;
    protected String _queueName;
    protected int _count;
    protected boolean _messageIds;
    protected boolean _persistent;
    protected int _size;
    protected int _threads;
    protected int _maxRecords;
    
    protected Connection _connection;
    protected Session _session;
    protected Destination _queue;
    protected String _client = "client";
    
    public Client(Properties props)
    {
        _props = props;
        
        init();
    }
    
    public void init()
    {
        _broker = _props.getProperty(BROKER);
        _maxRedelivery = Integer.parseInt(_props.getProperty(MAX_REDELIVERY));
        _maxPrefetch = Integer.parseInt(_props.getProperty(MAX_PREFETCH));
        _sessionType = getSessionType(_props.getProperty(SESSION));
        _transacted = _sessionType == Session.SESSION_TRANSACTED;
        _clientAck = _sessionType == Session.CLIENT_ACKNOWLEDGE;
        _queueName = _props.getProperty(QUEUE);
        _persistent = Boolean.parseBoolean(_props.getProperty(PERSISTENT));
        _count = Integer.parseInt(_props.getProperty(COUNT));
        _size = Integer.parseInt(_props.getProperty(SIZE));
        _messageIds = !Boolean.parseBoolean(_props.getProperty(MESSAGE_IDS));
        _threads = Integer.parseInt(_props.getProperty(THREADS));
        _maxRecords = Integer.parseInt(_props.getProperty(MAX_RECORDS));
    }
    
    public void shutdown()
    {
        try
        {
            _connection.close();
        }
        catch (JMSException e)
        {
            _log.error("failed shutting down the connection", e);
        }
    }
    
    public int getSessionType(String sessionType)
    {
        if (sessionType == null || sessionType.length() == 0)
        {
            throw new RuntimeException("empty or missing session property");
        }
        else if (sessionType.equalsIgnoreCase(SESSION_TRANSACTED))
        {
            return Session.SESSION_TRANSACTED;
        }
        else if (sessionType.equalsIgnoreCase(AUTO_ACKNOWLEDGE))
        {
            return Session.AUTO_ACKNOWLEDGE;
        }
        else if (sessionType.equalsIgnoreCase(CLIENT_ACKNOWLEDGE))
        {
            return Session.CLIENT_ACKNOWLEDGE;
        }
        else if (sessionType.equalsIgnoreCase(DUPS_OK_ACKNOWLEDGE))
        {
            return Session.DUPS_OK_ACKNOWLEDGE;
        }
        throw new RuntimeException("session property not recognised: " + sessionType);
    }
    
    public boolean connect()
    {
        String url = "amqp://guest:guest@" + _client + "/test?brokerlist='" + _broker + "'&maxprefetch='" + _maxPrefetch + "'&maxdeliverycount='" + _maxRedelivery + "'";
        System.setProperty(ClientProperties.MAX_DELIVERY_RECORDS_PROP_NAME, Integer.toString(_maxRecords));
        
        try
        {
            _connection = new AMQConnection(url);
            _session = _connection.createSession(_transacted, _sessionType);
            _queue = _session.createQueue(_queueName);
            _connection.setExceptionListener(new ExceptionListener()
            {
                public void onException(JMSException e)
                {
                    _log.error("jms exception received", e);
                    System.exit(0);
                }
            });
            return true;
        }
        catch (Exception e)
        {
            _log.error("Unable to setup connection, client and producer on broker", e);
            return false;
        }
    }
    
    public abstract void start() throws Exception;
    
    public Integer call() throws Exception {
        start();
        return -1;
    }
}