org.springframework.jms.listener.SessionAwareMessageListener Java Examples

The following examples show how to use org.springframework.jms.listener.SessionAwareMessageListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JmsContainerManagerImpl.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Constructor.
 */
@Autowired
public JmsContainerManagerImpl(final ProcessCache processCache,
                               final @Qualifier("daqInConnectionFactory") ConnectionFactory updateConnectionFactory,
                               final @Qualifier("sourceUpdateManager") SessionAwareMessageListener<Message> listener,
                               final @Qualifier("clusterCache") ClusterCache clusterCache,
                               final ThreadPoolTaskExecutor daqThreadPoolTaskExecutor,
                               final DaqProperties properties) {
  super();
  this.processCache = processCache;
  this.updateConnectionFactory = updateConnectionFactory;
  this.listener = listener;
  this.clusterCache = clusterCache;
  this.daqThreadPoolTaskExecutor = daqThreadPoolTaskExecutor;
  this.properties = properties;
}
 
Example #2
Source File: JmsContainerManagerTest.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Before
  public void setUp() throws Exception {
    mockProcessCache = EasyMock.createMock(ProcessCache.class);
    mockListener = EasyMock.createMock(SessionAwareMessageListener.class);
    mockClusterCache = EasyMock.createMock(ClusterCache.class);

    jmsSender = new ActiveJmsSender();
    JmsTemplate template = new JmsTemplate();
    template.setConnectionFactory(daqInConnectionFactory);
    template.setTimeToLive(60000);
    jmsSender.setJmsTemplate(template);

//    jmsContainerManager = new JmsContainerManagerImpl(mockProcessCache, daqInConnectionFactory, mockListener, mockClusterCache, new StandardEnvironment());
//    jmsContainerManager.setConsumersInitial(1);
//    jmsContainerManager.setConsumersMax(1);
//    jmsContainerManager.setNbExecutorThreads(2);
//    jmsContainerManager.setIdleTaskExecutionLimit(1);
//    jmsContainerManager.setMaxMessages(1);
//    jmsContainerManager.setReceiveTimeout(100);
//    jmsContainerManager.setJmsUpdateQueueTrunk(testTrunkName);
//    jmsContainerManager.setSessionTransacted(true);
//    jmsContainerManager.setUpdateWarmUpSeconds(60);
  }
 
Example #3
Source File: MessageListenerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Spring {@link SessionAwareMessageListener} entry point.
 * <p>Delegates the message to the target listener method, with appropriate
 * conversion of the message argument. If the target method returns a
 * non-null object, wrap in a JMS message and send it back.
 * @param message the incoming JMS message
 * @param session the JMS session to operate on
 * @throws JMSException if thrown by JMS API methods
 */
@Override
@SuppressWarnings("unchecked")
public void onMessage(Message message, @Nullable Session session) throws JMSException {
	// Check whether the delegate is a MessageListener impl itself.
	// In that case, the adapter will simply act as a pass-through.
	Object delegate = getDelegate();
	if (delegate != this) {
		if (delegate instanceof SessionAwareMessageListener) {
			Assert.state(session != null, "Session is required for SessionAwareMessageListener");
			((SessionAwareMessageListener<Message>) delegate).onMessage(message, session);
			return;
		}
		if (delegate instanceof MessageListener) {
			((MessageListener) delegate).onMessage(message);
			return;
		}
	}

	// Regular case: find a handler method reflectively.
	Object convertedMessage = extractMessage(message);
	String methodName = getListenerMethodName(message, convertedMessage);

	// Invoke the handler method with appropriate arguments.
	Object[] listenerArguments = buildListenerArguments(convertedMessage);
	Object result = invokeListenerMethod(methodName, listenerArguments);
	if (result != null) {
		handleResult(result, message, session);
	}
	else {
		logger.trace("No result object given - no result to handle");
	}
}
 
Example #4
Source File: JmsListenerContainerFactoryIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void invokeListener(JmsListenerEndpoint endpoint, Message message) throws JMSException {
	DefaultMessageListenerContainer messageListenerContainer = containerFactory.createListenerContainer(endpoint);
	Object listener = messageListenerContainer.getMessageListener();
	if (listener instanceof SessionAwareMessageListener) {
		((SessionAwareMessageListener<Message>) listener).onMessage(message, mock(Session.class));
	}
	else {
		((MessageListener) listener).onMessage(message);
	}
}
 
Example #5
Source File: MessageListenerAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Spring {@link SessionAwareMessageListener} entry point.
 * <p>Delegates the message to the target listener method, with appropriate
 * conversion of the message argument. If the target method returns a
 * non-null object, wrap in a JMS message and send it back.
 * @param message the incoming JMS message
 * @param session the JMS session to operate on
 * @throws JMSException if thrown by JMS API methods
 */
@Override
@SuppressWarnings("unchecked")
public void onMessage(Message message, @Nullable Session session) throws JMSException {
	// Check whether the delegate is a MessageListener impl itself.
	// In that case, the adapter will simply act as a pass-through.
	Object delegate = getDelegate();
	if (delegate != this) {
		if (delegate instanceof SessionAwareMessageListener) {
			Assert.state(session != null, "Session is required for SessionAwareMessageListener");
			((SessionAwareMessageListener<Message>) delegate).onMessage(message, session);
			return;
		}
		if (delegate instanceof MessageListener) {
			((MessageListener) delegate).onMessage(message);
			return;
		}
	}

	// Regular case: find a handler method reflectively.
	Object convertedMessage = extractMessage(message);
	String methodName = getListenerMethodName(message, convertedMessage);

	// Invoke the handler method with appropriate arguments.
	Object[] listenerArguments = buildListenerArguments(convertedMessage);
	Object result = invokeListenerMethod(methodName, listenerArguments);
	if (result != null) {
		handleResult(result, message, session);
	}
	else {
		logger.trace("No result object given - no result to handle");
	}
}
 
Example #6
Source File: JmsListenerContainerFactoryIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void invokeListener(JmsListenerEndpoint endpoint, Message message) throws JMSException {
	DefaultMessageListenerContainer messageListenerContainer = containerFactory.createListenerContainer(endpoint);
	Object listener = messageListenerContainer.getMessageListener();
	if (listener instanceof SessionAwareMessageListener) {
		((SessionAwareMessageListener<Message>) listener).onMessage(message, mock(Session.class));
	}
	else {
		((MessageListener) listener).onMessage(message);
	}
}
 
Example #7
Source File: JmsListenerContainerFactoryIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void invokeListener(JmsListenerEndpoint endpoint, Message message) throws JMSException {
	DefaultMessageListenerContainer messageListenerContainer = containerFactory.createListenerContainer(endpoint);
	Object listener = messageListenerContainer.getMessageListener();
	if (listener instanceof SessionAwareMessageListener) {
		((SessionAwareMessageListener<Message>) listener).onMessage(message, mock(Session.class));
	}
	else {
		((MessageListener) listener).onMessage(message);
	}
}
 
Example #8
Source File: MockServerConfig.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public MessageListenerContainer mockServerListener(ActiveMQConnectionFactory connectionFactory) {
  DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
  container.setConnectionFactory(connectionFactory);
  container.setDestination(new ActiveMQQueue("c2mon.client.request"));
  container.setMessageListener((SessionAwareMessageListener) (message, session) -> {
    session.createProducer(message.getJMSReplyTo()).send(session.createTextMessage("[]"));
  });
  return container;
}
 
Example #9
Source File: MQHierachy.java    From Thunder with Apache License 2.0 5 votes vote down vote up
public void listen(Destination destination, SessionAwareMessageListener<BytesMessage> messageListener, String requestSelector, boolean topic) throws Exception {
    int concurrentConsumers = mqPropertyEntity.getInteger(ThunderConstant.MQ_CONCURRENT_CONSUMERS_ATTRIBUTE_NAME);
    int maxConcurrentConsumers = mqPropertyEntity.getInteger(ThunderConstant.MQ_MAX_CONCURRENT_CONSUMERS_ATTRIBUTE_NAME);
    long receiveTimeout = mqPropertyEntity.getLong(ThunderConstant.MQ_RECEIVE_TIMEOUT_ATTRIBUTE_NAME);
    long recoveryInterval = mqPropertyEntity.getLong(ThunderConstant.MQ_RECOVERY_INTERVAL_ATTRIBUTE_NAME);
    int idleConsumerLimit = mqPropertyEntity.getInteger(ThunderConstant.MQ_IDLE_CONSUMER_LIMIT_ATTRIBUTE_NAME);
    int idleTaskExecutionLimit = mqPropertyEntity.getInteger(ThunderConstant.MQ_IDLE_TASK_EXECUTION_LIMIT_ATTRIBUTE_NAME);
    int cacheLevel = mqPropertyEntity.getInteger(ThunderConstant.MQ_CACHE_LEVEL_ATTRIBUTE_NAME);
    boolean acceptMessagesWhileStopping = mqPropertyEntity.getBoolean(ThunderConstant.MQ_ACCEPT_MESSAGES_WHILE_STOPPING_ATTRIBUTE_NAME);

    MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter();
    messageListenerAdapter.setDelegate(messageListener);

    DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
    messageListenerContainer.setDestination(destination);
    messageListenerContainer.setConnectionFactory(connectionFactory);
    messageListenerContainer.setMessageListener(messageListenerAdapter);
    if (StringUtils.isNotEmpty(requestSelector)) {
        messageListenerContainer.setMessageSelector(SelectorType.REQUEST_SELECTOR + " = '" + requestSelector + "'");
    }
    messageListenerContainer.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
    messageListenerContainer.setPubSubDomain(topic);
    messageListenerContainer.setConcurrentConsumers(topic ? 1 : concurrentConsumers);
    messageListenerContainer.setMaxConcurrentConsumers(topic ? 1 : maxConcurrentConsumers);
    messageListenerContainer.setReceiveTimeout(receiveTimeout);
    messageListenerContainer.setRecoveryInterval(recoveryInterval);
    messageListenerContainer.setIdleConsumerLimit(idleConsumerLimit);
    messageListenerContainer.setIdleTaskExecutionLimit(idleTaskExecutionLimit);
    messageListenerContainer.setCacheLevel(cacheLevel);
    messageListenerContainer.setAcceptMessagesWhileStopping(acceptMessagesWhileStopping);
    messageListenerContainer.afterPropertiesSet();
    messageListenerContainer.start();
}
 
Example #10
Source File: MessageListenerAdapter.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Spring {@link SessionAwareMessageListener} entry point.
 * <p>Delegates the message to the target listener method, with appropriate
 * conversion of the message argument. If the target method returns a
 * non-null object, wrap in a JMS message and send it back.
 * @param message the incoming JMS message
 * @param session the JMS session to operate on
 * @throws JMSException if thrown by JMS API methods
 */
@Override
@SuppressWarnings("unchecked")
public void onMessage(Message message, Session session) throws JMSException {
	// Check whether the delegate is a MessageListener impl itself.
	// In that case, the adapter will simply act as a pass-through.
	Object delegate = getDelegate();
	if (delegate != this) {
		if (delegate instanceof SessionAwareMessageListener) {
			if (session != null) {
				((SessionAwareMessageListener<Message>) delegate).onMessage(message, session);
				return;
			}
			else if (!(delegate instanceof MessageListener)) {
				throw new javax.jms.IllegalStateException("MessageListenerAdapter cannot handle a " +
						"SessionAwareMessageListener delegate if it hasn't been invoked with a Session itself");
			}
		}
		if (delegate instanceof MessageListener) {
			((MessageListener) delegate).onMessage(message);
			return;
		}
	}

	// Regular case: find a handler method reflectively.
	Object convertedMessage = extractMessage(message);
	String methodName = getListenerMethodName(message, convertedMessage);
	if (methodName == null) {
		throw new javax.jms.IllegalStateException("No default listener method specified: " +
				"Either specify a non-null value for the 'defaultListenerMethod' property or " +
				"override the 'getListenerMethodName' method.");
	}

	// Invoke the handler method with appropriate arguments.
	Object[] listenerArguments = buildListenerArguments(convertedMessage);
	Object result = invokeListenerMethod(methodName, listenerArguments);
	if (result != null) {
		handleResult(result, message, session);
	}
	else {
		logger.trace("No result object given - no result to handle");
	}
}