本文整理汇总了Java中javax.jms.MessageListener类的典型用法代码示例。如果您正苦于以下问题:Java MessageListener类的具体用法?Java MessageListener怎么用?Java MessageListener使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MessageListener类属于javax.jms包,在下文中一共展示了MessageListener类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: on_SUBSCRIBE
import javax.jms.MessageListener; //导入依赖的package包/类
@Test
public void on_SUBSCRIBE() throws JMSException {
final Session session = mock(Session.class);
ReflectionUtil.set(this.connection, "session", session);
final Destination destination = mock(Destination.class);
when(session.toDestination("/dest")).thenReturn(destination);
when(session.getConnection()).thenReturn(this.connection);
final MessageConsumer consumer = mock(MessageConsumer.class);
when(session.createConsumer(eq(destination), any(String.class))).thenReturn(consumer);
this.connection.on(new Message("ABC123", Frame.subscribe("1", "/dest").build()));
verify(this.log).info("Message received from client. [sessionId={},command={}]", "ABC123", Command.SUBSCRIBE);
verify(session).toDestination("/dest");
verify(session).getConnection();
verify(session).createConsumer(destination, "session IS NULL OR session = 'ABC123'");
verify(consumer).setMessageListener(any(MessageListener.class));
verifyNoMoreInteractions(session, destination, consumer);
}
示例2: setupJmsMessageContainerFullConfig
import javax.jms.MessageListener; //导入依赖的package包/类
@Test
public void setupJmsMessageContainerFullConfig() {
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
MessageListener messageListener = new MessageListenerAdapter();
SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
endpoint.setDestination("myQueue");
endpoint.setSelector("foo = 'bar'");
endpoint.setSubscription("mySubscription");
endpoint.setConcurrency("5-10");
endpoint.setMessageListener(messageListener);
endpoint.setupListenerContainer(container);
assertEquals("myQueue", container.getDestinationName());
assertEquals("foo = 'bar'", container.getMessageSelector());
assertEquals("mySubscription", container.getSubscriptionName());
assertEquals(5, container.getConcurrentConsumers());
assertEquals(10, container.getMaxConcurrentConsumers());
assertEquals(messageListener, container.getMessageListener());
}
示例3: createSimpleContainer
import javax.jms.MessageListener; //导入依赖的package包/类
@Test
public void createSimpleContainer() {
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
setDefaultJmsConfig(factory);
SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
MessageListener messageListener = new MessageListenerAdapter();
endpoint.setMessageListener(messageListener);
endpoint.setDestination("myQueue");
SimpleMessageListenerContainer container = factory.createListenerContainer(endpoint);
assertDefaultJmsConfig(container);
assertEquals(messageListener, container.getMessageListener());
assertEquals("myQueue", container.getDestinationName());
}
示例4: createJmsContainerFullConfig
import javax.jms.MessageListener; //导入依赖的package包/类
@Test
public void createJmsContainerFullConfig() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
setDefaultJmsConfig(factory);
factory.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER);
factory.setConcurrency("3-10");
factory.setMaxMessagesPerTask(5);
SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
MessageListener messageListener = new MessageListenerAdapter();
endpoint.setMessageListener(messageListener);
endpoint.setDestination("myQueue");
DefaultMessageListenerContainer container = factory.createListenerContainer(endpoint);
assertDefaultJmsConfig(container);
assertEquals(DefaultMessageListenerContainer.CACHE_CONSUMER, container.getCacheLevel());
assertEquals(3, container.getConcurrentConsumers());
assertEquals(10, container.getMaxConcurrentConsumers());
assertEquals(5, container.getMaxMessagesPerTask());
assertEquals(messageListener, container.getMessageListener());
assertEquals("myQueue", container.getDestinationName());
}
示例5: createJcaContainerFullConfig
import javax.jms.MessageListener; //导入依赖的package包/类
@Test
public void createJcaContainerFullConfig() {
DefaultJcaListenerContainerFactory factory = new DefaultJcaListenerContainerFactory();
setDefaultJcaConfig(factory);
factory.setConcurrency("10");
SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
MessageListener messageListener = new MessageListenerAdapter();
endpoint.setMessageListener(messageListener);
endpoint.setDestination("myQueue");
JmsMessageEndpointManager container = factory.createListenerContainer(endpoint);
assertDefaultJcaConfig(container);
assertEquals(10, container.getActivationSpecConfig().getMaxConcurrency());
assertEquals(messageListener, container.getMessageListener());
assertEquals("myQueue", container.getActivationSpecConfig().getDestinationName());
}
示例6: createSubscriber
import javax.jms.MessageListener; //导入依赖的package包/类
protected void createSubscriber(TopicSession topicSession, Topic topic) throws JMSException {
// Create subscriber
MessageConsumer subscriber = topicSession.createConsumer(topic);
// Attach message listener to subscriber
subscriber.setMessageListener(new MessageListener() {
public void onMessage(javax.jms.Message message) {
try {
// Process the message
processMessage(message);
} catch (Exception ex) {
// Error
logger.error("Error", ex);
}
}
});
}
示例7: setupBus
import javax.jms.MessageListener; //导入依赖的package包/类
public void setupBus(MessageListener msgLis) {
try {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL);
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createTopic("OPCUAMilo");
//Setup producer
producer = session.createProducer(destination);
//setup consumer
consumer = session.createConsumer(destination);
connection.start();
consumer.setMessageListener(msgLis);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
示例8: messageListener
import javax.jms.MessageListener; //导入依赖的package包/类
@Bean
public DefaultMessageListenerContainer messageListener() {
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
container.setConnectionFactory(this.connectionFactory);
container.setDestinationName("testQueue");
container.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
System.out.println(message.getBody(Object.class));
}
catch (JMSException ex) {
ex.printStackTrace();
}
}
});
return container;
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:19,代码来源:SampleHornetQApplication.java
示例9: receiveMessage
import javax.jms.MessageListener; //导入依赖的package包/类
public static void receiveMessage(final MessageListener listener, final String brokerHost, final String brokerPort, final String topic) throws JMSException {
// Create a ConnectionFactory
final ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(String.format("tcp://%s:%s", brokerHost, brokerPort));
// Create a Connection
final Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
final Destination destination = session.createQueue(topic);
// register MessageListener
final MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(listener);
}
示例10: receiveMessage
import javax.jms.MessageListener; //导入依赖的package包/类
public static String receiveMessage(MessageListener listener, String brokerHost, String brokerPort, String topic) throws JMSException {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(String.format("tcp://%s:%s", brokerHost, brokerPort));
// 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(topic);
// register MessageListener
MessageConsumer consumer = session.createConsumer(destination);
Message message = consumer.receive();
if (message instanceof TextMessage) {
return ((TextMessage) message).getText();
} else {
return null;
}
}
示例11: createConsumer
import javax.jms.MessageListener; //导入依赖的package包/类
/**
* Creates a {@link MessageConsumerResources} with a dedicated
* {@link Session} required for transacted and InOut consumers.
*/
private MessageConsumerResources createConsumer() throws Exception {
MessageConsumerResources answer;
Connection conn = getConnectionResource().borrowConnection();
try {
Session session = conn.createSession(isTransacted(), isTransacted() ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE);
Destination destination = getEndpoint().getDestinationCreationStrategy().createDestination(session, getDestinationName(), isTopic());
MessageConsumer messageConsumer = JmsObjectFactory.createMessageConsumer(session, destination, getMessageSelector(), isTopic(), getDurableSubscriptionId());
MessageListener handler = createMessageHandler(session);
messageConsumer.setMessageListener(handler);
answer = new MessageConsumerResources(session, messageConsumer);
} catch (Exception e) {
log.error("Unable to create the MessageConsumer", e);
throw e;
} finally {
getConnectionResource().returnConnection(conn);
}
return answer;
}
示例12: setMessageListener
import javax.jms.MessageListener; //导入依赖的package包/类
/**
* Set message listener
*
* @param listener The listener
* @throws JMSException Thrown if an error occurs
*/
@Override
public void setMessageListener(final MessageListener listener) throws JMSException {
session.lock();
try {
checkState();
session.checkStrict();
if (listener == null) {
consumer.setMessageListener(null);
} else {
consumer.setMessageListener(wrapMessageListener(listener));
}
} finally {
session.unlock();
}
}
示例13: setMessageListener
import javax.jms.MessageListener; //导入依赖的package包/类
/**
* Sets the message consumer's <CODE>MessageListener</CODE>.
* <P>
* Setting the message listener to null is the equivalent of unsetting the
* message listener for the message consumer.
* <P>
* The effect of calling <CODE>MessageConsumer.setMessageListener</CODE>
* while messages are being consumed by an existing listener or the consumer
* is being used to consume messages synchronously is undefined.
*
* @param listener the listener to which the messages are to be delivered
* @throws JMSException if the JMS provider fails to receive the next
* message due to some internal error.
* @see javax.jms.MessageConsumer#getMessageListener
*/
@Override
public void setMessageListener(MessageListener listener) throws JMSException {
checkClosed();
if (info.getPrefetchSize() == 0) {
throw new JMSException("Illegal prefetch size of zero. This setting is not supported for asynchronous consumers please set a value of at least 1");
}
if (listener != null) {
boolean wasRunning = session.isRunning();
if (wasRunning) {
session.stop();
}
this.messageListener.set(listener);
session.redispatch(this, unconsumedMessages);
if (wasRunning) {
session.start();
}
} else {
this.messageListener.set(null);
}
}
示例14: receiveMessagesFromQueue
import javax.jms.MessageListener; //导入依赖的package包/类
/**
* To receive a message from a queue.
*
* @throws JMSException JMS Exception.
* @throws InterruptedException Interrupted exception while waiting in between messages.
*/
public void receiveMessagesFromQueue() throws JMSException, InterruptedException {
QueueConnection queueConn = (QueueConnection) connectionFactory.createConnection();
QueueSession queueSession = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = queueSession.createQueue(JMSTestConstants.QUEUE_NAME_1);
MessageConsumer queueReceiver = queueSession.createConsumer(destination);
MessageListener listener = message -> {
try {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
logger.info("Message text received : " + (textMessage.getText()));
}
} catch (JMSException e) {
logger.info("JMS exception occurred.");
}
};
queueReceiver.setMessageListener(listener);
queueConn.start();
}
示例15: setupQueueListener
import javax.jms.MessageListener; //导入依赖的package包/类
/**
* Create A Consumer for the given Queue and set the given
* {@link MessageListener}.
*
* @param destination The destination of the {@link Queue}
* @param listener The {@link MessageListener} to set
* @return true means failure
*/
public synchronized boolean setupQueueListener(final String destination, final MessageListener listener) {
this.checkAllowedState(State.SESSION_ACTIVE);
final Destination dest = new ActiveMQQueue(destination);
MessageConsumer consumer;
try {
consumer = this.session.createConsumer(dest);
consumer.setMessageListener(listener);
}
catch (final JMSException ex) {
LOGGER.error("Problems during setup of QueueListener.", ex);
this.state = State.FAILURE;
return true;
}
return false;
}