summaryrefslogtreecommitdiff
path: root/qpid/java/perftests/src/main/java/org/apache/qpid/client/message/TestMessageFactory.java
blob: 3ad6c021bd0c35f1792d2c60ce8df94eedc9582a (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
/*
 *
 * 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.client.message;

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.SimpleByteBufferAllocator;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.ObjectMessage;
import javax.jms.StreamMessage;
import javax.jms.BytesMessage;
import javax.jms.TextMessage;
import javax.jms.DeliveryMode;
import javax.jms.Destination;

public class TestMessageFactory
{
    private static final String MESSAGE_DATA_BYTES = "-message payload-message paylaod-message payload-message paylaod";

    public static TextMessage newTextMessage(Session session, int size) throws JMSException
    {
        return session.createTextMessage(createMessagePayload(size));
    }

    public static TextMessage newJMSTextMessage(Session session, int size, String encoding) throws JMSException
    {

        TextMessage message = session.createTextMessage();
        message.clearBody();
        message.setText(createMessagePayload(size));
        return message;
    }

    public static BytesMessage newBytesMessage(Session session, int size) throws JMSException
    {
        BytesMessage message = session.createBytesMessage();
        message.writeUTF(createMessagePayload(size));
        return message;
    }

    public static StreamMessage newStreamMessage(Session session, int size) throws JMSException
    {
        StreamMessage message = session.createStreamMessage();
        message.writeString(createMessagePayload(size));
        return message;
    }

    public static ObjectMessage newObjectMessage(Session session, int size) throws JMSException
    {
        if (size == 0)
        {
            return session.createObjectMessage();
        }
        else
        {
            return session.createObjectMessage(createMessagePayload(size));
        }
    }

    /**
     * Creates an ObjectMessage with given size and sets the JMS properties (JMSReplyTo and DeliveryMode)
     * @param session
     * @param replyDestination
     * @param size
     * @param persistent
     * @return the new ObjectMessage
     * @throws JMSException
     */
    public static ObjectMessage newObjectMessage(Session session, Destination replyDestination, int size, boolean persistent) throws JMSException
    {
        ObjectMessage msg = newObjectMessage(session, size);

        // Set the messages persistent delivery flag.
        msg.setJMSDeliveryMode(persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);

        // Ensure that the temporary reply queue is set as the reply to destination for the message.
        if (replyDestination != null)
        {
            msg.setJMSReplyTo(replyDestination);
        }

        return msg;
    }

    public static String createMessagePayload(int size)
    {
        StringBuffer buf = new StringBuffer(size);
        int count = 0;
        while (count <= (size - MESSAGE_DATA_BYTES.length()))
        {
            buf.append(MESSAGE_DATA_BYTES);
            count += MESSAGE_DATA_BYTES.length();
        }
        if (count < size)
        {
            buf.append(MESSAGE_DATA_BYTES, 0, size - count);
        }

        return buf.toString();
    }
}