001 /*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 *
020 */
021 package org.apache.qpid.client.message;
022
023 import org.apache.mina.common.ByteBuffer;
024 import org.apache.mina.common.SimpleByteBufferAllocator;
025
026 import javax.jms.JMSException;
027 import javax.jms.Session;
028 import javax.jms.ObjectMessage;
029 import javax.jms.StreamMessage;
030 import javax.jms.BytesMessage;
031 import javax.jms.TextMessage;
032 import javax.jms.DeliveryMode;
033 import javax.jms.Destination;
034
035 public class TestMessageFactory
036 {
037 private static final String MESSAGE_DATA_BYTES = "-message payload-message paylaod-message payload-message paylaod";
038
039 public static TextMessage newTextMessage(Session session, int size) throws JMSException
040 {
041 return session.createTextMessage(createMessagePayload(size));
042 }
043
044 public static TextMessage newJMSTextMessage(Session session, int size, String encoding) throws JMSException
045 {
046
047 TextMessage message = session.createTextMessage();
048 message.clearBody();
049 message.setText(createMessagePayload(size));
050 return message;
051 }
052
053 public static BytesMessage newBytesMessage(Session session, int size) throws JMSException
054 {
055 BytesMessage message = session.createBytesMessage();
056 message.writeUTF(createMessagePayload(size));
057 return message;
058 }
059
060 public static StreamMessage newStreamMessage(Session session, int size) throws JMSException
061 {
062 StreamMessage message = session.createStreamMessage();
063 message.writeString(createMessagePayload(size));
064 return message;
065 }
066
067 public static ObjectMessage newObjectMessage(Session session, int size) throws JMSException
068 {
069 if (size == 0)
070 {
071 return session.createObjectMessage();
072 }
073 else
074 {
075 return session.createObjectMessage(createMessagePayload(size));
076 }
077 }
078
079 /**
080 * Creates an ObjectMessage with given size and sets the JMS properties (JMSReplyTo and DeliveryMode)
081 * @param session
082 * @param replyDestination
083 * @param size
084 * @param persistent
085 * @return the new ObjectMessage
086 * @throws JMSException
087 */
088 public static ObjectMessage newObjectMessage(Session session, Destination replyDestination, int size, boolean persistent) throws JMSException
089 {
090 ObjectMessage msg = newObjectMessage(session, size);
091
092 // Set the messages persistent delivery flag.
093 msg.setJMSDeliveryMode(persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
094
095 // Ensure that the temporary reply queue is set as the reply to destination for the message.
096 if (replyDestination != null)
097 {
098 msg.setJMSReplyTo(replyDestination);
099 }
100
101 return msg;
102 }
103
104 public static String createMessagePayload(int size)
105 {
106 StringBuffer buf = new StringBuffer(size);
107 int count = 0;
108 while (count <= (size - MESSAGE_DATA_BYTES.length()))
109 {
110 buf.append(MESSAGE_DATA_BYTES);
111 count += MESSAGE_DATA_BYTES.length();
112 }
113 if (count < size)
114 {
115 buf.append(MESSAGE_DATA_BYTES, 0, size - count);
116 }
117
118 return buf.toString();
119 }
120 }
|