summaryrefslogtreecommitdiff
path: root/qpid/java/client/src/old_test/java/org/apache/qpid/multiconsumer/AMQTest.java
blob: db02b9954a8ae8be559680a50bdb92235538368d (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 *
 * 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.multiconsumer;

import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

import javax.jms.Connection;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import javax.jms.Topic;

import junit.framework.TestCase;

import org.apache.commons.codec.binary.Base64;
import org.apache.qpid.client.AMQConnectionFactory;
import org.apache.qpid.client.AMQConnection;
import org.apache.qpid.client.AMQTopic;
import org.apache.qpid.exchange.ExchangeDefaults;
import org.apache.qpid.framing.AMQShortString;
import org.apache.qpid.jms.Session;

/**
 * Test AMQ.
 */
public class AMQTest extends TestCase implements ExceptionListener
{

    private final static String COMPRESSION_PROPNAME = "_MSGAPI_COMP";
    private final static String UTF8 = "UTF-8";
    private static final String SUBJECT = "test.amq";
    private static final String DUMMYCONTENT = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String HUGECONTENT;

    private AMQConnection connect = null;
    private Session pubSession = null;
    private Session subSession = null;
    private Topic topic = null;

    static
    {
        StringBuilder sb = new StringBuilder(DUMMYCONTENT.length() * 115);
        for (int i = 0; i < 100; i++)
        {
            sb.append(DUMMYCONTENT);
        }
        HUGECONTENT = sb.toString();
    }

    private void setup() throws Exception
    {
        connect = new AMQConnection("localhost", 5672, "guest", "guest", "client1", "/");
        connect.setExceptionListener(this);
        pubSession = connect.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
        subSession = connect.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
        topic = new AMQTopic(pubSession.getDefaultTopicExchangeName(), new AMQShortString(SUBJECT));

        connect.start();
    }

    public void testMultipleListeners() throws Exception
    {
        setup();
        try
        {
            // Create 5 listeners
            MsgHandler[] listeners = new MsgHandler[5];
            for (int i = 0; i < listeners.length; i++)
            {
                listeners[i] = new MsgHandler();
                MessageConsumer subscriber = subSession.createConsumer(topic);
                subscriber.setMessageListener(listeners[i]);
            }
            MessageProducer publisher = pubSession.createProducer(topic);
            // Send a single message
            TextMessage msg = pubSession.createTextMessage();
            msg.setText(DUMMYCONTENT);
            publisher.send(msg);
            Thread.sleep(5000);
            // Check listeners to ensure they all got it
            for (int i = 0; i < listeners.length; i++)
            {
                if (listeners[i].isGotIt())
                {
                    System.out.println("Got callback for listener " + i);
                }
                else
                {
                    TestCase.fail("Listener " + i + " did not get callback");
                }
            }
        }
        catch (Throwable e)
        {
            System.err.println("Error: " + e);
            e.printStackTrace(System.err);
        }
        finally
        {
            close();
        }
    }

    public void testCompression() throws Exception
    {
        setup();
        String comp = this.compressString(HUGECONTENT);
        try
        {
            MsgHandler listener = new MsgHandler();
            MessageConsumer subscriber = subSession.createConsumer(topic);
            subscriber.setMessageListener(listener);
            MessageProducer publisher = pubSession.createProducer(topic);

            // Send a single message
            TextMessage msg = pubSession.createTextMessage();
            // Set the compressed text
            msg.setText(comp);
            msg.setBooleanProperty(COMPRESSION_PROPNAME, true);
            publisher.send(msg);
            Thread.sleep(1000);
            // Check listeners to ensure we got it
            if (listener.isGotIt())
            {
                System.out.println("Got callback for listener");
            }
            else
            {
                TestCase.fail("Listener did not get callback");
            }
        }
        finally
        {
            close();
        }
    }

    private void close() throws Exception
    {
        if (connect != null)
        {
            connect.close();
        }
    }

    private class MsgHandler implements MessageListener
    {
        private boolean gotIt = false;

        public void onMessage(Message msg)
        {
            try
            {
                TextMessage textMessage = (TextMessage) msg;
                String string = textMessage.getText();
                if (string != null && string.length() > 0)
                {
                    gotIt = true;
                }
                if (textMessage.getBooleanProperty(COMPRESSION_PROPNAME))
                {
                    string = inflateString(string);
                } 
                System.out.println("Got callback of size " + (string==null?0:string.length()));
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        public boolean isGotIt()
        {
            return this.gotIt;
        }
    }

    private String compressString(String string) throws Exception
    {
        long start = System.currentTimeMillis();
        byte[] input = string.getBytes();
        Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION);
        compressor.setInput(input);
        compressor.finish();

        // Get byte array from output of compressor
        ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length);
        byte[] buf = new byte[1024];
        while (!compressor.finished())
        {
            int cnt = compressor.deflate(buf);
            baos.write(buf, 0, cnt);
        }
        baos.close();
        byte[] output = baos.toByteArray();

        // Convert byte array into String
        byte[] base64 = Base64.encodeBase64(output);
        String sComp = new String(base64, UTF8);

        long diff = System.currentTimeMillis() - start;
        System.out.println("Compressed text from " + input.length + " to "
                + sComp.getBytes().length + " in " + diff + " ms");
        System.out.println("Compressed text = '" + sComp + "'");

        return sComp;
    }

    private String inflateString(String string) throws Exception
    {
        byte[] input = string.getBytes();

        // First convert Base64 string back to binary array
        byte[] bytes = Base64.decodeBase64(input);

        // Set string as input data for decompressor
        Inflater decompressor = new Inflater();
        decompressor.setInput(bytes);

        // Decompress the data
        ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
        byte[] buf = new byte[1024];
        while (!decompressor.finished())
        {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        }
        bos.close();
        byte[] output = bos.toByteArray();

        // Get the decompressed data
        return new String(output, UTF8);
    }

    /**
     * @see javax.jms.ExceptionListener#onException(javax.jms.JMSException)
     */
    public void onException(JMSException e)
    {
        System.err.println(e.getMessage());
        e.printStackTrace(System.err);
    }


}