本文整理匯總了Java中javax.jms.Session.createConsumer方法的典型用法代碼示例。如果您正苦於以下問題:Java Session.createConsumer方法的具體用法?Java Session.createConsumer怎麽用?Java Session.createConsumer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.jms.Session
的用法示例。
在下文中一共展示了Session.createConsumer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testFailedCreateConsumerConnectionStillWorks
import javax.jms.Session; //導入方法依賴的package包/類
@Test
public void testFailedCreateConsumerConnectionStillWorks() throws JMSException {
Connection connection = pooledConnFact.createConnection("guest", "password");
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(name.getMethodName());
try {
session.createConsumer(queue);
fail("Should fail to create consumer");
} catch (JMSSecurityException ex) {
LOG.info("Caught expected security error");
}
queue = session.createQueue("GUESTS." + name.getMethodName());
MessageProducer producer = session.createProducer(queue);
producer.close();
connection.close();
}
示例2: testReceive
import javax.jms.Session; //導入方法依賴的package包/類
@Test
public void testReceive() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
Session session = connection.createSession();
Queue queue = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(queue, "Color = Red");
assertNull(consumer.receive());
consumer.close();
try {
consumer.receive();
fail("Should not be able to interact with closed consumer");
} catch (IllegalStateException ise) {}
}
示例3: testReceiveNoWait
import javax.jms.Session; //導入方法依賴的package包/類
@Test
public void testReceiveNoWait() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
Session session = connection.createSession();
Queue queue = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(queue, "Color = Red");
assertNull(consumer.receiveNoWait());
consumer.close();
try {
consumer.receiveNoWait();
fail("Should not be able to interact with closed consumer");
} catch (IllegalStateException ise) {}
}
示例4: testReceiveTimed
import javax.jms.Session; //導入方法依賴的package包/類
@Test
public void testReceiveTimed() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
Session session = connection.createSession();
Queue queue = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(queue, "Color = Red");
assertNull(consumer.receive(1));
consumer.close();
try {
consumer.receive(1);
fail("Should not be able to interact with closed consumer");
} catch (IllegalStateException ise) {}
}
示例5: testGetMessageSelector
import javax.jms.Session; //導入方法依賴的package包/類
@Test
public void testGetMessageSelector() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
Session session = connection.createSession();
Queue queue = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(queue, "Color = Red");
assertNotNull(consumer.getMessageSelector());
assertEquals("Color = Red", consumer.getMessageSelector());
consumer.close();
try {
consumer.getMessageSelector();
fail("Should not be able to interact with closed consumer");
} catch (IllegalStateException ise) {}
}
示例6: consumeMessage
import javax.jms.Session; //導入方法依賴的package包/類
/** Deletes the input Message from the given Queue, by creating a temporary consumer for that Message
* @param session the JMS Session.
* @param message the JMS Message.
* @param queueName the Queue to consume from.
* @throws FrameworkException in case any internal error occurs.
* @throws ApplicationExceptions Indicates application error(s).
*/
static void consumeMessage(Session session, Message message, String queueName) throws FrameworkException, ApplicationExceptions {
try {
// Creates a consumer on the session for the given queueName, and specifying a selector having HEADER_JMS_MESSAGE_ID as the given messageId
String selector = new StringBuilder(HEADER_JMS_MESSAGE_ID)
.append("='")
.append(message.getJMSMessageID())
.append('\'')
.toString();
MessageConsumer consumer = session.createConsumer(JmsClientHelper.obtainQueue(queueName), selector);
// Consume the message. Wait for 10 seconds at most
Message m = consumer.receive(10000);
if (m == null)
throw new ApplicationExceptions(new JaffaMessagingApplicationException(JaffaMessagingApplicationException.MESSAGE_NOT_FOUND));
consumer.close();
} catch (JMSException e) {
log.error("Error in consuming a JMS Message", e);
throw new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.DELETE_ERROR, null, e);
}
}
示例7: doCleanupQueue
import javax.jms.Session; //導入方法依賴的package包/類
private void doCleanupQueue( final Session session, final Destination destination ) throws JMSException {
try {
MessageConsumer consumer = session.createConsumer(destination);
Message message = null;
do {
message = consumer.receiveNoWait();
if (message != null) {
message.acknowledge();
}
} while (message != null);
} finally {
releaseSession(false);
}
}
示例8: startListener
import javax.jms.Session; //導入方法依賴的package包/類
private void startListener(MessageHandler handler) {
log.debug("Starting listener...");
try {
ActiveMQConnection connection = ActiveMqUtils.openConnection(user, password, url);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(queueInput);
MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(message -> handleMessage(handler, message, session));
log.debug("Listening to {}", queueInput);
} catch (Exception e) {
log.error("Error starting listening to queue {}", queueInput, e);
}
}
示例9: startListeningToTopic
import javax.jms.Session; //導入方法依賴的package包/類
/**
* Start listening for messages on topic
*
* @param topicName the topic name
*/
@PublicAtsApi
public void startListeningToTopic(
final String topicName ) {
final TopicInfo topicInfo = getTopicInfo(topicName);
if (topicInfo.isListening()) {
throw new JmsMessageException("We are already listening for messages on topic " + topicName);
}
try {
final Session session = loadSession(false, Session.AUTO_ACKNOWLEDGE);
final Topic topic = session.createTopic(topicName);
topicInfo.topicConsumer = session.createConsumer(topic);
topicInfo.topicConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage( Message message ) {
topicInfo.addMessage(message);
}
});
} catch (JMSException e) {
throw new JmsMessageException("Could not start listening for messages on topic " + topicName,
e);
}
}
示例10: testLingeringPooledSessionsHoldingPrefetchedMessages
import javax.jms.Session; //導入方法依賴的package包/類
@Test(timeout = 60000)
public void testLingeringPooledSessionsHoldingPrefetchedMessages() throws Exception {
produceMessages();
Session pooledSession1 = pooledConn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
pooledSession1.createConsumer(queue);
final QueueViewMBean view = getProxyToQueue(queue.getPhysicalName());
assertTrue("Should have all sent messages in flight:", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisfied() throws Exception {
return view.getInFlightCount() == MESSAGE_COUNT;
}
}, TimeUnit.SECONDS.toMillis(20), TimeUnit.MILLISECONDS.toMillis(25)));
// While all the message are in flight we should get anything on this consumer.
Session session = directConn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queue);
assertNull(consumer.receive(500));
pooledConn1.close();
assertTrue("Should have only one consumer now:", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisfied() throws Exception {
return view.getSubscriptions().length == 1;
}
}, TimeUnit.SECONDS.toMillis(20), TimeUnit.MILLISECONDS.toMillis(25)));
// Now we'd expect that the message stuck in the prefetch of the pooled session's
// consumer would be rerouted to the non-pooled session's consumer.
assertNotNull(consumer.receive(10000));
}
示例11: testNonPooledConnectionCloseNotHoldingPrefetchedMessages
import javax.jms.Session; //導入方法依賴的package包/類
@Test(timeout = 60000)
public void testNonPooledConnectionCloseNotHoldingPrefetchedMessages() throws Exception {
produceMessages();
Session directSession = directConn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
directSession.createConsumer(queue);
final QueueViewMBean view = getProxyToQueue(queue.getPhysicalName());
assertTrue("Should have all sent messages in flight:", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisfied() throws Exception {
return view.getInFlightCount() == MESSAGE_COUNT;
}
}, TimeUnit.SECONDS.toMillis(20), TimeUnit.MILLISECONDS.toMillis(25)));
// While all the message are in flight we should get anything on this consumer.
Session session = directConn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queue);
assertNull(consumer.receive(500));
directConn2.close();
assertTrue("Should have only one consumer now:", Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisfied() throws Exception {
return view.getSubscriptions().length == 1;
}
}, TimeUnit.SECONDS.toMillis(20), TimeUnit.MILLISECONDS.toMillis(25)));
// Now we'd expect that the message stuck in the prefetch of the first session's
// consumer would be rerouted to the alternate session's consumer.
assertNotNull(consumer.receive(10000));
}
示例12: sqsMessageConsumer
import javax.jms.Session; //導入方法依賴的package包/類
@Bean
public MessageConsumer sqsMessageConsumer(SQSConnection connection) throws JMSException {
/*
* Create the session and use CLIENT_ACKNOWLEDGE mode. Acknowledging
* messages deletes them from the queue
*/
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
return session.createConsumer(session.createQueue(queueName));
}
示例13: requeueFailedMessages
import javax.jms.Session; //導入方法依賴的package包/類
@Override
public void requeueFailedMessages() {
try {
ActiveMQConnection connection = ActiveMqUtils.openConnection(user, password, url);
Session session = ActiveMqUtils.startSession(connection);
int count = getQueueSize(session, queueError);
if (count < 1) {
return;
}
log.info("Requeuing {} failed messages...", count);
Queue queueErr = session.createQueue(queueError);
MessageConsumer consumer = session.createConsumer(queueErr);
Queue queueRetry = session.createQueue(queueInput);
MessageProducer producer = session.createProducer(queueRetry);
for (int consumed = 0; consumed < count; consumed++) {
TextMessage message = (TextMessage) consumer.receive(REQUEUE_TIMEOUT);
if (message == null) {
continue;
}
String text = message.getText();
String requestId = message.getJMSCorrelationID();
log.info("Requeuing message '{}'", text);
try {
TextMessage newMessage = session.createTextMessage(text);
newMessage.setJMSCorrelationID(requestId);
producer.send(newMessage);
} catch (Exception e) {
log.error("Failed to requeue message", e);
}
message.acknowledge();
session.commit();
}
producer.close();
consumer.close();
} catch (JMSException ex) {
throw new MessageQueueException("Failed to requeue failed messages", ex);
}
}
示例14: validateFactoryCreationWithActiveMQLibraries
import javax.jms.Session; //導入方法依賴的package包/類
/**
* This test simply validates that {@link ConnectionFactory} can be setup by pointing to the location of the client
* libraries at runtime. It uses ActiveMQ which is not present at the POM but instead pulled from Maven repo using
* {@link TestUtils#setupActiveMqLibForTesting(boolean)}, which implies that for this test to run the computer must
* be connected to the Internet. If computer is not connected to the Internet, this test will quietly fail logging a
* message.
*/
@Test
public void validateFactoryCreationWithActiveMQLibraries() throws Exception {
try {
String libPath = TestUtils.setupActiveMqLibForTesting(true);
TestRunner runner = TestRunners.newTestRunner(mock(Processor.class));
JNDIConnectionFactoryProvider cfProvider = new JNDIConnectionFactoryProvider();
runner.addControllerService("cfProvider", cfProvider);
runner.setProperty(cfProvider, JNDIConnectionFactoryProvider.BROKER_URI,
"vm://localhost?broker.persistent=false");
runner.setProperty(cfProvider, JNDIConnectionFactoryProvider.JNDI_CF_LOOKUP, "ConnectionFactory");
runner.setProperty(cfProvider, JNDIConnectionFactoryProvider.CLIENT_LIB_DIR_PATH, libPath);
runner.setProperty(cfProvider, JNDIConnectionFactoryProvider.CONNECTION_FACTORY_IMPL,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
runner.enableControllerService(cfProvider);
runner.assertValid(cfProvider);
Connection connection = cfProvider.getConnectionFactory().createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination queue = session.createQueue("myqueue");
MessageProducer producer = session.createProducer(queue);
MessageConsumer consumer = session.createConsumer(queue);
TextMessage message = session.createTextMessage("Hello");
producer.send(message);
assertEquals("Hello", ((TextMessage) consumer.receive()).getText());
connection.stop();
connection.close();
} catch (Exception e) {
logger.error("'validateFactoryCreationWithActiveMQLibraries' failed due to ", e);
}
}
示例15: main
import javax.jms.Session; //導入方法依賴的package包/類
public static void main(String[] args) {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://Toshiba:61616");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("HELLOWORLD.TESTQ");
// Create a MessageConsumer from the Session to the Topic or Queue
MessageConsumer consumer = session.createConsumer(destination);
// Wait for a message
Message message = consumer.receive(1000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Received: " + text);
} else {
System.out.println("Received: " + message);
}
consumer.close();
session.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}