本文整理汇总了Java中javax.jms.JMSConsumer.receive方法的典型用法代码示例。如果您正苦于以下问题:Java JMSConsumer.receive方法的具体用法?Java JMSConsumer.receive怎么用?Java JMSConsumer.receive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.jms.JMSConsumer
的用法示例。
在下文中一共展示了JMSConsumer.receive方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSendStreamMessage
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test
public void testSendStreamMessage() throws JMSException, InterruptedException {
JmsProducerCompletionListenerTest.CountingCompletionListener cl = new JmsProducerCompletionListenerTest.CountingCompletionListener(1);
JMSProducer producer = context.createProducer();
producer.setAsync(cl);
StreamMessage msg = context.createStreamMessage();
msg.setStringProperty("name", name.getMethodName());
String bprop = "booleanProp";
String iprop = "intProp";
msg.setBooleanProperty(bprop, true);
msg.setIntProperty(iprop, 42);
msg.writeBoolean(true);
msg.writeInt(67);
producer.send(queue1, msg);
JMSConsumer consumer = context.createConsumer(queue1);
Message msg2 = consumer.receive(100);
Assert.assertNotNull(msg2);
Assert.assertTrue(cl.completionLatch.await(1, TimeUnit.SECONDS));
StreamMessage sm = (StreamMessage) cl.lastMessage;
Assert.assertEquals(true, sm.getBooleanProperty(bprop));
Assert.assertEquals(42, sm.getIntProperty(iprop));
Assert.assertEquals(true, sm.readBoolean());
Assert.assertEquals(67, sm.readInt());
}
示例2: testReceive
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test
public void testReceive() throws JMSException {
JMSConsumer consumer = context.createConsumer(context.createTemporaryQueue());
assertNull(consumer.receive());
consumer.close();
try {
consumer.receive();
fail("Should not be able to interact with closed consumer");
} catch (IllegalStateRuntimeException ise) {}
}
示例3: testReceiveTimed
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test
public void testReceiveTimed() throws JMSException {
JMSConsumer consumer = context.createConsumer(context.createTemporaryQueue());
assertNull(consumer.receive(1));
consumer.close();
try {
consumer.receive(1);
fail("Should not be able to interact with closed consumer");
} catch (IllegalStateRuntimeException ise) {}
}
示例4: testCloseSecondContextConnectionRemainsOpen
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test
public void testCloseSecondContextConnectionRemainsOpen() throws JMSException {
JMSContext localContext = context.createContext(JMSContext.CLIENT_ACKNOWLEDGE);
Assert.assertEquals("client_ack", JMSContext.CLIENT_ACKNOWLEDGE, localContext.getSessionMode());
JMSProducer producer = localContext.createProducer();
JMSConsumer consumer = localContext.createConsumer(queue1);
final int pass = 1;
for (int idx = 0; idx < 2; idx++) {
Message m = localContext.createMessage();
int intProperty = random.nextInt();
m.setIntProperty("random", intProperty);
Assert.assertNotNull(m);
producer.send(queue1, m);
m = null;
Message msg = consumer.receive(100);
Assert.assertNotNull("must have a msg", msg);
Assert.assertEquals(intProperty, msg.getIntProperty("random"));
/* In the second pass we close the connection before ack'ing */
if (idx == pass) {
localContext.close();
}
/**
* From {@code JMSContext.close()}'s javadoc:<br/>
* Invoking the {@code acknowledge} method of a received message from a closed connection's
* session must throw an {@code IllegalStateRuntimeException}. Closing a closed connection
* must NOT throw an exception.
*/
try {
msg.acknowledge();
Assert.assertEquals("connection should be open on pass 0. It is " + pass, 0, idx);
} catch (javax.jms.IllegalStateException expected) {
// HORNETQ-1209 "JMS 2.0" XXX JMSContext javadoc says we must expect a
// IllegalStateRuntimeException here. But Message.ack...() says it must throws the
// non-runtime variant.
Assert.assertEquals("we only close the connection on pass " + pass, pass, idx);
}
}
}
示例5: testSendAndReceiveOnAutoCreatedTopicJMS2
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testSendAndReceiveOnAutoCreatedTopicJMS2() throws Exception {
ConnectionFactory cf = new JmsConnectionFactory(getBrokerQpidJMSConnectionURI());
JMSContext context = cf.createContext();
String topicName = UUID.randomUUID().toString();
SimpleString simpleTopicName = SimpleString.toSimpleString(topicName);
try {
Topic topic = context.createTopic(topicName);
JMSProducer producer = context.createProducer();
TextMessage message = context.createTextMessage("test-message");
// this will auto-create the address, but not the subscription queue
producer.send(topic, message);
assertNotNull(server.getAddressInfo(simpleTopicName));
assertEquals(RoutingType.MULTICAST, server.getAddressInfo(simpleTopicName).getRoutingType());
assertTrue(server.getAddressInfo(simpleTopicName).isAutoCreated());
assertTrue(server.getPostOffice().getBindingsForAddress(simpleTopicName).getBindings().isEmpty());
// this will auto-create the subscription queue
JMSConsumer consumer = context.createConsumer(topic);
assertFalse(server.getPostOffice().getBindingsForAddress(simpleTopicName).getBindings().isEmpty());
producer.send(topic, message);
context.start();
message = (TextMessage) consumer.receive(1000);
assertNotNull(message);
assertNotNull(message.getText());
assertEquals("test-message", message.getText());
consumer.close();
assertTrue(server.getPostOffice().getBindingsForAddress(simpleTopicName).getBindings().isEmpty());
} finally {
context.close();
}
}
示例6: receiveMessages
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
protected final void receiveMessages(JMSConsumer consumer, final int start, final int msgCount, final boolean ack) {
try {
for (int i = start; i < msgCount; i++) {
Message message = consumer.receive(100);
Assert.assertNotNull("Expecting a message " + i, message);
final int actual = message.getIntProperty("counter");
Assert.assertEquals("expected=" + i + ". Got: property['counter']=" + actual, i, actual);
if (ack)
message.acknowledge();
}
} catch (JMSException cause) {
throw new JMSRuntimeException(cause.getMessage(), cause.getErrorCode(), cause);
}
}
示例7: testReceiveMessageWithReceiveZeroTimeout
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test(timeout = 20000)
public void testReceiveMessageWithReceiveZeroTimeout() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer);
testPeer.expectBegin();
Queue queue = context.createQueue("myQueue");
DescribedType amqpValueNullContent = new AmqpValueDescribedType(null);
testPeer.expectReceiverAttach();
testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueNullContent);
testPeer.expectDispositionThatIsAcceptedAndSettled();
JMSConsumer messageConsumer = context.createConsumer(queue);
Message receivedMessage = messageConsumer.receive(0);
assertNotNull("A message should have been recieved", receivedMessage);
testPeer.expectEnd();
testPeer.expectClose();
context.close();
testPeer.waitForAllHandlersToComplete(2000);
}
}
示例8: main
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
public static void main(String[] args) {
JMSConsumer consumer;
/*
* Create client-acknowledge context for receiver.
* Receive message and process it.
* Acknowledge message.
*/
try (JMSContext context =
connectionFactory.createContext(JMSContext.CLIENT_ACKNOWLEDGE);) {
System.out.println(
"Created client-acknowledge JMSContext");
consumer = context.createConsumer(queue);
while (true) {
Message m = consumer.receive(1000);
if (m != null) {
if (m instanceof TextMessage) {
// Comment out the following two lines to receive
// a large volume of messages
System.out.println(
"Reading message: " + m.getBody(String.class));
System.out.println(
"Acknowledging TextMessage");
context.acknowledge();
} else {
System.out.println(
"Acknowledging non-text control message");
context.acknowledge();
break;
}
}
}
} catch (JMSException e) {
System.err.println("Exception occurred: " + e.toString());
}
}
示例9: multipleSendsUsingSetters
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test
public void multipleSendsUsingSetters() throws Exception {
server.createQueue(SimpleString.toSimpleString("q1"), RoutingType.ANYCAST, SimpleString.toSimpleString("q1"), null, true, false);
Queue q1 = context.createQueue("q1");
context.createProducer().setProperty("prop1", 1).setProperty("prop2", 2).send(q1, "Text1");
context.createProducer().setProperty("prop1", 3).setProperty("prop2", 4).send(q1, "Text2");
for (int i = 0; i < 100; i++) {
context.createProducer().send(q1, "Text" + i);
}
ActiveMQSession sessionUsed = (ActiveMQSession) (((ActiveMQJMSContext) context).getUsedSession());
ClientSessionImpl coreSession = (ClientSessionImpl) sessionUsed.getCoreSession();
// JMSConsumer is supposed to cache the producer, each call to createProducer is supposed to always return the same producer
assertEquals(1, coreSession.cloneProducers().size());
JMSConsumer consumer = context.createConsumer(q1);
TextMessage text = (TextMessage) consumer.receive(5000);
assertNotNull(text);
assertEquals("Text1", text.getText());
assertEquals(1, text.getIntProperty("prop1"));
assertEquals(2, text.getIntProperty("prop2"));
text = (TextMessage) consumer.receive(5000);
assertNotNull(text);
assertEquals("Text2", text.getText());
assertEquals(3, text.getIntProperty("prop1"));
assertEquals(4, text.getIntProperty("prop2"));
for (int i = 0; i < 100; i++) {
assertEquals("Text" + i, consumer.receiveBody(String.class, 1000));
}
consumer.close();
context.close();
}
示例10: testDelay
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test
public void testDelay() throws Exception {
JMSProducer producer = context.createProducer();
JMSConsumer consumer = context.createConsumer(queue1);
producer.setDeliveryDelay(500);
long timeStart = System.currentTimeMillis();
String strRandom = newXID().toString();
producer.send(queue1, context.createTextMessage(strRandom));
TextMessage msg = (TextMessage) consumer.receive(2500);
assertNotNull(msg);
long actualDelay = System.currentTimeMillis() - timeStart;
assertTrue("delay is not working, actualDelay=" + actualDelay, actualDelay >= 500 && actualDelay < 2000);
assertEquals(strRandom, msg.getText());
}
示例11: testGroupingWithJMS2Producer
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
@Test
public void testGroupingWithJMS2Producer() throws Exception {
ConnectionFactory fact = getCF();
Assume.assumeFalse("only makes sense withOUT auto-group", ((ActiveMQConnectionFactory) fact).isAutoGroup());
Assume.assumeTrue("only makes sense withOUT explicit group-id", ((ActiveMQConnectionFactory) fact).getGroupID() == null);
final String groupID = UUID.randomUUID().toString();
JMSContext ctx = addContext(getCF().createContext(JMSContext.SESSION_TRANSACTED));
JMSProducer producer = ctx.createProducer().setProperty("JMSXGroupID", groupID);
JMSConsumer consumer1 = ctx.createConsumer(queue);
JMSConsumer consumer2 = ctx.createConsumer(queue);
JMSConsumer consumer3 = ctx.createConsumer(queue);
ctx.start();
for (int j = 0; j < 100; j++) {
TextMessage message = ctx.createTextMessage("Message" + j);
producer.send(queue, message);
String prop = message.getStringProperty("JMSXGroupID");
assertNotNull(prop);
assertEquals(groupID, prop);
}
ctx.commit();
//All msgs should go to the first consumer
for (int j = 0; j < 100; j++) {
TextMessage tm = (TextMessage) consumer1.receive(10000);
assertNotNull(tm);
tm.acknowledge();
assertEquals("Message" + j, tm.getText());
assertEquals(tm.getStringProperty("JMSXGroupID"), groupID);
tm = (TextMessage) consumer2.receiveNoWait();
assertNull(tm);
tm = (TextMessage) consumer3.receiveNoWait();
assertNull(tm);
}
ctx.commit();
ctx.close();
}
示例12: main
import javax.jms.JMSConsumer; //导入方法依赖的package包/类
public static void main(String[] args) {
MapMessage outMessage;
JMSConsumer orderConfirmReceiver;
MapMessage inMessage;
if (args.length != 1) {
System.out.println("Error: Program takes numerical argument.");
System.exit(1);
}
quantity = (new Integer(args[0])).intValue();
System.out.println("Retailer: Quantity to be ordered is " + quantity);
/*
* Create non-transacted context and sender for
* vendor order queue.
* Create message to vendor, setting item and
* quantity values.
* Send message.
* Create receiver for retailer confirmation queue.
* Get message and report result.
* Send an end-of-message-stream message so vendor
* will stop processing orders.
*/
try (JMSContext context = connectionFactory.createContext();) {
outMessage = context.createMapMessage();
outMessage.setString("Item", "Computer(s)");
outMessage.setInt("Quantity", quantity);
outMessage.setJMSReplyTo(retailerConfirmQueue);
context.createProducer().send(vendorOrderQueue, outMessage);
System.out.println(
"Retailer: Ordered " + quantity + " computer(s)");
orderConfirmReceiver = context.createConsumer(
retailerConfirmQueue);
inMessage = (MapMessage) orderConfirmReceiver.receive();
if (inMessage.getBoolean("OrderAccepted") == true) {
System.out.println("Retailer: Order filled");
} else {
System.out.println("Retailer: Order not filled");
}
System.out.println("Retailer: Placing another order");
outMessage.setInt("Quantity", quantity * 2);
context.createProducer().send(vendorOrderQueue, outMessage);
System.out.println(
"Retailer: Ordered " + outMessage.getInt("Quantity")
+ " computer(s)");
inMessage = (MapMessage) orderConfirmReceiver.receive();
if (inMessage.getBoolean("OrderAccepted") == true) {
System.out.println("Retailer: Order filled");
} else {
System.out.println("Retailer: Order not filled");
}
/*
* Send a non-text control message indicating end
* of messages.
*/
context.createProducer().send(vendorOrderQueue,
context.createMessage());
} catch (JMSException e) {
System.err.println(
"Retailer: Exception occurred: " + e.toString());
}
}