本文整理汇总了Java中javax.jms.JMSConsumer.receiveBody方法的典型用法代码示例。如果您正苦于以下问题:Java JMSConsumer.receiveBody方法的具体用法?Java JMSConsumer.receiveBody怎么用?Java JMSConsumer.receiveBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.jms.JMSConsumer
的用法示例。
在下文中一共展示了JMSConsumer.receiveBody方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sharedDurableSubSimpleRoundRobin
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test
public void sharedDurableSubSimpleRoundRobin() throws Exception {
context = cf.createContext();
try {
JMSConsumer con1 = context.createSharedDurableConsumer(topic1, "mySharedCon");
JMSConsumer con2 = context.createSharedDurableConsumer(topic1, "mySharedCon");
context.start();
JMSProducer producer = context.createProducer();
int numMessages = 10;
for (int i = 0; i < numMessages; i++) {
producer.send(topic1, "msg:" + i);
}
for (int i = 0; i < numMessages; i += 2) {
String msg = con1.receiveBody(String.class, 5000);
System.out.println("msg = " + msg);
msg = con2.receiveBody(String.class, 5000);
System.out.println("msg = " + msg);
}
} finally {
context.close();
}
}
示例2: testReceiveBody
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test
public void testReceiveBody() throws JMSException {
JMSConsumer consumer = context.createConsumer(context.createTemporaryQueue());
try {
consumer.receiveBody(String.class);
fail("Should not be able to interact with closed consumer");
} catch (JMSRuntimeException ise) {}
}
示例3: testReceiveBodyTimed
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test
public void testReceiveBodyTimed() throws JMSException {
JMSConsumer consumer = context.createConsumer(context.createTemporaryQueue());
try {
consumer.receiveBody(String.class, 1);
fail("Should not be able to interact with closed consumer");
} catch (JMSRuntimeException ise) {}
}
示例4: deleteMessage
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
public void deleteMessage(int orderID) throws Exception {
JMSConsumer consumer = context.createConsumer(queue, "OrderID='" + orderID + "'");
CustomerOrder order = consumer.receiveBody(CustomerOrder.class, 1);
if (order != null) {
logger.log(Level.INFO, "Order {0} removed from queue.", order.getId());
} else {
logger.log(Level.SEVERE, "Order {0} was not removed from queue!", orderID);
throw new Exception("Order not removed from queue");
}
}
示例5: processOrder
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
public CustomerOrder processOrder(String OrderMessageID) {
logger.log(Level.INFO, "Processing Order {0}", OrderMessageID);
JMSConsumer consumer = context.createConsumer(queue, "JMSMessageID='" + OrderMessageID + "'");
CustomerOrder order = consumer.receiveBody(CustomerOrder.class, 1);
return order;
}
示例6: testReceiveBodyTextMessage
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test(timeout = 20000)
public void testReceiveBodyTextMessage() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer);
testPeer.expectBegin();
final String content = "Message-Content";
Queue queue = context.createQueue("myQueue");
DescribedType amqpValueContent = new AmqpValueDescribedType(content);
testPeer.expectReceiverAttach();
testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueContent);
testPeer.expectDispositionThatIsAcceptedAndSettled();
testPeer.expectEnd();
testPeer.expectClose();
JMSConsumer messageConsumer = context.createConsumer(queue);
String received = messageConsumer.receiveBody(String.class, 3000);
assertNotNull(received);
assertEquals(content, received);
context.close();
testPeer.waitForAllHandlersToComplete(3000);
}
}
示例7: testReceiveBodyBytesMessage
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test(timeout = 20000)
public void testReceiveBodyBytesMessage() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer);
testPeer.expectBegin();
Queue queue = context.createQueue("myQueue");
PropertiesDescribedType properties = new PropertiesDescribedType();
properties.setContentType(Symbol.valueOf(AmqpMessageSupport.OCTET_STREAM_CONTENT_TYPE));
MessageAnnotationsDescribedType msgAnnotations = null;
msgAnnotations = new MessageAnnotationsDescribedType();
msgAnnotations.setSymbolKeyedAnnotation(AmqpMessageSupport.JMS_MSG_TYPE, AmqpMessageSupport.JMS_BYTES_MESSAGE);
final byte[] expectedContent = "expectedContent".getBytes();
DescribedType dataContent = new DataDescribedType(new Binary(expectedContent));
testPeer.expectReceiverAttach();
testPeer.expectLinkFlowRespondWithTransfer(null, msgAnnotations, properties, null, dataContent);
testPeer.expectDispositionThatIsAcceptedAndSettled();
JMSConsumer messageConsumer = context.createConsumer(queue);
byte[] received = messageConsumer.receiveBody(byte[].class, 3000);
testPeer.waitForAllHandlersToComplete(3000);
assertNotNull(received);
assertTrue(Arrays.equals(expectedContent, received));
testPeer.expectEnd();
testPeer.expectClose();
context.close();
testPeer.waitForAllHandlersToComplete(3000);
}
}
示例8: doTestReceiveBodyFailsDoesNotAcceptMessage
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
public void doTestReceiveBodyFailsDoesNotAcceptMessage(int sessionMode) throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer);
testPeer.expectBegin();
final String content = "Message-Content";
Queue queue = context.createQueue("myQueue");
DescribedType amqpValueContent = new AmqpValueDescribedType(content);
testPeer.expectReceiverAttach();
testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueContent);
testPeer.expectEnd();
testPeer.expectClose();
JMSConsumer messageConsumer = context.createConsumer(queue);
try {
messageConsumer.receiveBody(Boolean.class, 3000);
fail("Should not read as Boolean type");
} catch (MessageFormatRuntimeException mfre) {
}
context.close();
testPeer.waitForAllHandlersToComplete(3000);
}
}
示例9: deleteMessage
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
public void deleteMessage(int orderID) throws Exception {
JMSConsumer consumer = context.createConsumer(queue, "OrderID='" + orderID + "'") ;
CustomerOrder order = consumer.receiveBody(CustomerOrder.class, 1);
if (order != null)
logger.log(Level.INFO, "Order {0} removed from queue.", order.getId());
else {
logger.log(Level.SEVERE, "Order {0} was not removed from queue!", orderID);
throw new Exception("Order not removed from queue");
}
}
示例10: main
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
public static void main(final String[] args) throws Exception {
InitialContext initialContext = null;
JMSContext jmsContext = null;
JMSContext jmsContext2 = null;
try {
// Step 1. Create an initial context to perform the JNDI lookup.
initialContext = new InitialContext();
// Step 2. Perfom a lookup on the queue
Topic topic = (Topic) initialContext.lookup("topic/exampleTopic");
// Step 3. Perform a lookup on the Connection Factory
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
// Step 4.Create a JMS Context
jmsContext = cf.createContext();
// Step 5. Create a message producer.
JMSProducer producer = jmsContext.createProducer();
// Step 6. Create a shared consumer
JMSConsumer jmsConsumer = jmsContext.createSharedConsumer(topic, "sc1");
// Step 7. Create a second JMS Context for a second shared consumer
jmsContext2 = cf.createContext();
// Step 8. Create the second shared consumer
JMSConsumer jmsConsumer2 = jmsContext2.createSharedConsumer(topic, "sc1");
// Step 9. send 2 messages
producer.send(topic, "this is a String!");
producer.send(topic, "this is a second String!");
// Step 10. receive the messages shared by both consumers
String body = jmsConsumer.receiveBody(String.class, 5000);
System.out.println("body = " + body);
body = jmsConsumer2.receiveBody(String.class, 5000);
System.out.println("body = " + body);
} finally {
// Step 11. Be sure to close our JMS resources!
if (initialContext != null) {
initialContext.close();
}
if (jmsContext != null) {
jmsContext.close();
}
if (jmsContext2 != null) {
jmsContext2.close();
}
}
}
示例11: testShareDuraleWithJMSContext
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test
public void testShareDuraleWithJMSContext() throws Exception {
((ActiveMQConnectionFactory) cf).setConsumerWindowSize(0);
JMSContext conn = cf.createContext(JMSContext.AUTO_ACKNOWLEDGE);
JMSConsumer consumer = conn.createSharedDurableConsumer(topic, "c1");
JMSProducer producer = conn.createProducer();
for (int i = 0; i < 100; i++) {
producer.setProperty("count", i).send(topic, "test" + i);
}
JMSContext conn2 = conn.createContext(JMSContext.AUTO_ACKNOWLEDGE);
JMSConsumer consumer2 = conn2.createSharedDurableConsumer(topic, "c1");
for (int i = 0; i < 50; i++) {
String txt = consumer.receiveBody(String.class, 5000);
System.out.println("TXT:" + txt);
Assert.assertNotNull(txt);
txt = consumer.receiveBody(String.class, 5000);
System.out.println("TXT:" + txt);
Assert.assertNotNull(txt);
}
Assert.assertNull(consumer.receiveNoWait());
Assert.assertNull(consumer2.receiveNoWait());
boolean exceptionHappened = false;
try {
conn.unsubscribe("c1");
} catch (Exception e) {
e.printStackTrace();
exceptionHappened = true;
}
Assert.assertTrue(exceptionHappened);
consumer.close();
consumer2.close();
conn2.close();
conn.unsubscribe("c1");
}
示例12: testReceiveBodyMapMessage
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test(timeout = 20000)
public void testReceiveBodyMapMessage() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer);
testPeer.expectBegin();
Queue queue = context.createQueue("myQueue");
// Prepare an AMQP message for the test peer to send, containing an
// AmqpValue section holding a map with entries for each supported type,
// and annotated as a JMS map message.
String myBoolKey = "myBool";
boolean myBool = true;
String myByteKey = "myByte";
byte myByte = 4;
String myBytesKey = "myBytes";
byte[] myBytes = myBytesKey.getBytes();
String myCharKey = "myChar";
char myChar = 'd';
String myDoubleKey = "myDouble";
double myDouble = 1234567890123456789.1234;
String myFloatKey = "myFloat";
float myFloat = 1.1F;
String myIntKey = "myInt";
int myInt = Integer.MAX_VALUE;
String myLongKey = "myLong";
long myLong = Long.MAX_VALUE;
String myShortKey = "myShort";
short myShort = 25;
String myStringKey = "myString";
String myString = myStringKey;
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put(myBoolKey, myBool);
map.put(myByteKey, myByte);
map.put(myBytesKey, new Binary(myBytes));// the underlying AMQP message uses Binary rather than byte[] directly.
map.put(myCharKey, myChar);
map.put(myDoubleKey, myDouble);
map.put(myFloatKey, myFloat);
map.put(myIntKey, myInt);
map.put(myLongKey, myLong);
map.put(myShortKey, myShort);
map.put(myStringKey, myString);
MessageAnnotationsDescribedType msgAnnotations = new MessageAnnotationsDescribedType();
msgAnnotations.setSymbolKeyedAnnotation(AmqpMessageSupport.JMS_MSG_TYPE, AmqpMessageSupport.JMS_MAP_MESSAGE);
DescribedType amqpValueSectionContent = new AmqpValueDescribedType(map);
// receive the message from the test peer
testPeer.expectReceiverAttach();
testPeer.expectLinkFlowRespondWithTransfer(null, msgAnnotations, null, null, amqpValueSectionContent);
testPeer.expectDispositionThatIsAcceptedAndSettled();
testPeer.expectEnd();
testPeer.expectClose();
JMSConsumer messageConsumer = context.createConsumer(queue);
@SuppressWarnings("unchecked")
Map<String, Object> receivedMap = messageConsumer.receiveBody(Map.class, 3000);
// verify the content is as expected
assertNotNull("Map was not received", receivedMap);
assertEquals("Unexpected boolean value", myBool, receivedMap.get(myBoolKey));
assertEquals("Unexpected byte value", myByte, receivedMap.get(myByteKey));
byte[] readBytes = (byte[]) receivedMap.get(myBytesKey);
assertTrue("Read bytes were not as expected: " + Arrays.toString(readBytes), Arrays.equals(myBytes, readBytes));
assertEquals("Unexpected char value", myChar, receivedMap.get(myCharKey));
assertEquals("Unexpected double value", myDouble, (double) receivedMap.get(myDoubleKey), 0.0);
assertEquals("Unexpected float value", myFloat, (float) receivedMap.get(myFloatKey), 0.0);
assertEquals("Unexpected int value", myInt, receivedMap.get(myIntKey));
assertEquals("Unexpected long value", myLong, receivedMap.get(myLongKey));
assertEquals("Unexpected short value", myShort, receivedMap.get(myShortKey));
assertEquals("Unexpected UTF value", myString, receivedMap.get(myStringKey));
context.close();
testPeer.waitForAllHandlersToComplete(3000);
}
}
示例13: testReceiveBodyObjectMessage
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test(timeout = 20000)
public void testReceiveBodyObjectMessage() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer);
testPeer.expectBegin();
Queue queue = context.createQueue("myQueue");
PropertiesDescribedType properties = new PropertiesDescribedType();
properties.setContentType(Symbol.valueOf(AmqpMessageSupport.SERIALIZED_JAVA_OBJECT_CONTENT_TYPE));
String expectedContent = "expectedContent";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(expectedContent);
oos.flush();
oos.close();
byte[] bytes = baos.toByteArray();
MessageAnnotationsDescribedType msgAnnotations = new MessageAnnotationsDescribedType();
msgAnnotations.setSymbolKeyedAnnotation(AmqpMessageSupport.JMS_MSG_TYPE, AmqpMessageSupport.JMS_OBJECT_MESSAGE);
DescribedType dataContent = new DataDescribedType(new Binary(bytes));
testPeer.expectReceiverAttach();
testPeer.expectLinkFlowRespondWithTransfer(null, msgAnnotations, properties, null, dataContent);
testPeer.expectDispositionThatIsAcceptedAndSettled();
testPeer.expectEnd();
testPeer.expectClose();
JMSConsumer messageConsumer = context.createConsumer(queue);
String received = messageConsumer.receiveBody(String.class, 3000);
assertNotNull(received);
assertEquals(expectedContent, received);
context.close();
testPeer.waitForAllHandlersToComplete(3000);
}
}
示例14: doTestReceiveBodyFailsThenCalledWithCorrectType
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
public void doTestReceiveBodyFailsThenCalledWithCorrectType(int sessionMode) throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer);
testPeer.expectBegin();
final String content = "Message-Content";
Queue queue = context.createQueue("myQueue");
DescribedType amqpValueContent = new AmqpValueDescribedType(content);
testPeer.expectReceiverAttach();
testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueContent);
JMSConsumer messageConsumer = context.createConsumer(queue);
try {
messageConsumer.receiveBody(Boolean.class, 3000);
fail("Should not read as Boolean type");
} catch (MessageFormatRuntimeException mfre) {
}
testPeer.waitForAllHandlersToComplete(3000);
if (sessionMode == JMSContext.AUTO_ACKNOWLEDGE ||
sessionMode == JMSContext.DUPS_OK_ACKNOWLEDGE) {
testPeer.expectDispositionThatIsAcceptedAndSettled();
}
String received = messageConsumer.receiveBody(String.class, 3000);
if (sessionMode == JMSContext.AUTO_ACKNOWLEDGE ||
sessionMode == JMSContext.DUPS_OK_ACKNOWLEDGE) {
assertNotNull(received);
assertEquals(content, received);
} else {
assertNull(received);
}
testPeer.expectEnd();
testPeer.expectClose();
context.close();
testPeer.waitForAllHandlersToComplete(3000);
}
}