javaspringjboss-mdb

How to set many unique MDB's instances


I am using Spring 3.1

I have configured HornetQ and MDB which listening to it.

My target is to inject to those MDBS a propertyclass which will have unique properties.

Each MDB will have it's own properties

as soon as I try it I get exception.

Thats my code:

MDB:

public class FeedListenerMDB implements MessageListener
{
    private final static Logger log = Logger.getLogger(FeedListenerMDB.class);

    FeedPropertiesDTO feedPropertiesDTO;

    ...



    @Override
    public void onMessage(Message message)
    {
        if (message instanceof TextMessage)
        {
            try
            {
                String text = ((TextMessage) message).getText();
                log.debug("i am marketmaker=" + feedPropertiesDTO.getMarketMakerId() + " and the message I got=" + text);
            }
            catch (JMSException ex)
            {
                throw new RuntimeException(ex);
            }
        }
        else
        {
            throw new IllegalArgumentException("Message must be of type TextMessage");
        }
    }

}

Thats FeedPropertiesDTO :

public class FeedPropertiesDTO
{
    private String marketMakerId;

    public String getMarketMakerId()
    {
        return marketMakerId;
    }

    public void setMarketMakerId(String marketMakerId)
    {
        this.marketMakerId = marketMakerId;
    }

}

now this is my application context beans settings:

<bean id="FeedListenerMarketMaker1MDB" class="com.fixgw.mdb.FeedListenerMDB">
        <property name="feedPropertiesDTO" ref="feedListenerMarketMaker1Properties" />
    </bean>

    <bean id="feedListenerMarketMaker1Properties" class="com.fixgw.dto.FeedPropertiesDTO">
        <property name="marketMakerId" value="1" />
    </bean>


    <bean id="FeedListenerMarketMaker2MDB" class="com.fixgw.mdb.FeedListenerMDB">
        <property name="feedPropertiesDTO" ref="feedListenerMarketMaker2Properties" />
    </bean>


    <bean id="feedListenerMarketMaker2Properties" class="com.fixgw.dto.FeedPropertiesDTO" >
        <property name="marketMakerId" value="2" />
    </bean>

In this way Ill be able to configure my MDBS from the applicationContext and add new ones in the future without touching the code.

But I get this error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.fixgw.mdb.FeedListenerMDB] is defined: expected single matching bean but found 2: [FeedListenerMarketMaker1MDB, FeedListenerMarketMaker2MDB]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:800)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:551)
    ... 17 more

Any idea how I could achieve what I need?

thanks, ray.


Solution

  • The problem is not in the code you have shown here. Somewhere in your code, you have something like this:

    @Autowired
    private FeedListenerMDB feedListenerMDB;
    

    Spring can't resolve this dependency because there are two candidates available.

    Use a @Qualifier annotation or be more specific in your requirement.