git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1351743 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2012-06-19 15:32:32 +00:00
parent e0bb36b4b7
commit 482be54f82
2 changed files with 89 additions and 2 deletions

View File

@ -23,6 +23,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.management.openmbean.ArrayType;
@ -284,6 +285,7 @@ public final class OpenTypeSupport {
try {
byte preview[] = new byte[(int)Math.min(length, 255)];
m.readBytes(preview);
m.reset();
// This is whack! Java 1.5 JMX spec does not support primitive
// arrays!

View File

@ -16,14 +16,81 @@
*/
package org.apache.activemq.jmx;
import static org.junit.Assert.assertEquals;
import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.jmx.CompositeDataConstants;
import org.apache.activemq.broker.jmx.OpenTypeSupport;
import org.apache.activemq.broker.jmx.QueueViewMBean;
import org.apache.activemq.command.ActiveMQBytesMessage;
import org.apache.activemq.command.ActiveMQQueue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OpenTypeSupportTest {
private static final Logger LOG = LoggerFactory.getLogger(OpenTypeSupportTest.class);
private static BrokerService brokerService;
private static String TESTQUEUE = "testQueue";
private static ActiveMQConnectionFactory connectionFactory;
private static String BYTESMESSAGE_TEXT = "This is a short text";
private static String BROKER_ADDRESS = "tcp://localhost:0";
private static ActiveMQQueue queue = new ActiveMQQueue(TESTQUEUE);
private String connectionUri;
@Before
public void setUp() throws Exception {
brokerService = new BrokerService();
brokerService.setPersistent(false);
brokerService.setUseJmx(true);
connectionUri = brokerService.addConnector(BROKER_ADDRESS).getPublishableConnectString();
brokerService.start();
connectionFactory = new ActiveMQConnectionFactory(connectionUri);
sendMessage();
}
@After
public void tearDown() throws Exception {
brokerService.stop();
brokerService.waitUntilStopped();
}
private static void sendMessage() throws JMSException {
Connection conn = connectionFactory.createConnection();
try {
conn.start();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination queue = session.createQueue(TESTQUEUE);
BytesMessage toSend = session.createBytesMessage();
toSend.writeBytes(BYTESMESSAGE_TEXT.getBytes());
MessageProducer producer = session.createProducer(queue);
producer.send(queue, toSend);
} finally {
conn.close();
}
}
@Test
public void bytesMessagePreview() throws Exception {
QueueViewMBean queue = getProxyToQueueViewMBean();
assertEquals(extractText(queue.browse()[0]), extractText(queue.browse()[0]));
}
@Test
public void testBrowseByteMessageFails() throws Exception {
ActiveMQBytesMessage bm = new ActiveMQBytesMessage();
@ -31,4 +98,22 @@ public class OpenTypeSupportTest {
Object result = OpenTypeSupport.convert(bm);
LOG.info("result : " + result);
}
private String extractText(CompositeData message) {
Byte content[] = (Byte[]) message.get(CompositeDataConstants.BODY_PREVIEW);
byte out[] = new byte[content.length];
for (int i = 0; i < content.length; i++) {
out[i] = content[i];
}
return new String(out);
}
private QueueViewMBean getProxyToQueueViewMBean() throws MalformedObjectNameException, JMSException {
ObjectName queueViewMBeanName = new ObjectName(
"org.apache.activemq" + ":Type=Queue,Destination=" +
queue.getQueueName() + ",BrokerName=localhost");
QueueViewMBean proxy = (QueueViewMBean)
brokerService.getManagementContext().newProxyInstance(queueViewMBeanName, QueueViewMBean.class, true);
return proxy;
}
}