Following example with Spring's SessionAwareMessageListener

View: New views
2 Messages — Rating Filter:   Alert me  

Following example with Spring's SessionAwareMessageListener

by tnine :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all,
  I'm writing a quick app for testing MDP's with Spring 2.5, Active MQ 5.2.0, and Jencks 2.1.  I've used the spring context and the MessageListener below.  I'd like to utilize the SessionAwareMessageListener interface in Spring so I can have access to the session object.  Is there a way to configure this with spring and Jencks?  I can't seem to find any documentation on what I need, so any help would be appreciated.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config />
    <context:component-scan
        base-package="com.onwebconsulting.messaging" />

    <context:property-placeholder location="file:${properties-file}" />


    <bean id="jencks" class="org.jencks.JCAContainer">
        <property name="threadPoolSize" value="25" />
        <property name="transactionManager" ref="transactionManager" />


        <!-- the JCA Resource Adapter -->
        <property name="resourceAdapter">
            <bean id="activeMQResourceAdapter"
                class="org.apache.activemq.ra.ActiveMQResourceAdapter">
                <property name="serverUrl" value="${mq.broker.url}" />
            </bean>
        </property>
    </bean>



    <bean id="inboundConnectorA" class="org.jencks.JCAConnector">
        <property name="jcaContainer" ref="jencks" />

        <!-- subscription details -->
        <property name="activationSpec">
            <bean
                class="org.apache.activemq.ra.ActiveMQActivationSpec">
                <property name="destination" value="${mq.queue.name}" />
                <property name="destinationType"
                    value="javax.jms.Queue" />
            </bean>
        </property>

        <property name="transactionManager" ref="transactionManager"></property>

        <property name="ref" value="listener" />
    </bean>

    <bean id="transactionManager"
        class="org.jencks.factory.TransactionManagerFactoryBean" />


</beans>

Listener class

package com.onwebconsulting.messaging.reader.jms;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Service;

import com.onwebconsulting.messaging.reader.MessageReceiver;

/**
 *
 * @author Todd Nine
 *
 */
@Service("listener")
public class TextMessageReceiver implements MessageListener,
        SessionAwareMessageListener, MessageReceiver {

    /*
     * (non-Javadoc)
     *
     * @see org.springframework.jms.listener.SessionAwareMessageListener#onMessage(javax.jms.Message,
     *      javax.jms.Session)
     */
    public void onMessage(Message message, Session session) throws JMSException {

        if (!(message instanceof TextMessage)) {
            session.rollback();
            return;
        }

        TextMessage txtMessage = (TextMessage) message;

        receiveMessage(txtMessage.getText());

    }

    /*
     * (non-Javadoc)
     *
     * @see com.onwebconsulting.messaging.reader.MessageReceiver#receiveMessage(java.lang.String)
     */
    public void receiveMessage(String inputMessage) {
        System.out.println(inputMessage);

    }

    public void onMessage(Message message) {

        if (!(message instanceof TextMessage)) {
            throw new RuntimeException("Incorrect message type");
        }

        TextMessage txtMessage = (TextMessage) message;

        try {
            receiveMessage(txtMessage.getText());
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }

    }

}



Thanks,
Todd

Re: Following example with Spring's SessionAwareMessageListener

by Andrei Ivanov-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Dec 2, 2008 at 3:56 AM, Todd Nine <todd.nine@...> wrote:
> Hi all,
>   I'm writing a quick app for testing MDP's with Spring 2.5, Active MQ
> 5.2.0, and Jencks 2.1.  I've used the spring context and the MessageListener
> below.  I'd like to utilize the SessionAwareMessageListener interface in
> Spring so I can have access to the session object.  Is there a way to
> configure this with spring and Jencks?  I can't seem to find any
> documentation on what I need, so any help would be appreciated.

I think that is really a Spring question...
Your listener is obtained from a bean factory, in
org.jencks.DefaultEndpointFactory:

MessageListener messageListener = (MessageListener)
beanFactory.getBean(ref, MessageListener.class);

You have to find out how/when Spring applies its bean factory post processors.
Maybe you have to make your listener instantiate eagerly (not lazy).

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email