Search code examples
javaspringjmsmessage-driven-bean

How to set MDB programmatic and dynamically


I am using Spring3.1 on standalone env.

I set topic with jms templates this way:

<bean id="mm1sessionsTopicSendingTemplate" class="org.springframework.jndi.JndiObjectFactoryBean"
    depends-on="jmsServerManagerImpl">
    <property name="jndiName">
        <value>/topic/mm1sessionsTopic</value>
    </property>
</bean>

For this topic I set MDB with DefaultMessageListenerContainer this way:

<bean id="mm1sessionDispatcherListener"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="pubSubDomain" value="true" />
        <property name="concurrentConsumers" value="1" />
        <property name="destination" ref="mm1sessionsTopicSendingTemplate" />
        <property name="messageListener" ref="mm1SessionMDB" />
        <property name="sessionAcknowledgeModeName" value="AUTO_ACKNOWLEDGE" />
    </bean>

In this way I must set mm1SessionMDB in advanced via xml:

<bean id="mm1SessionMDB" class="com.mdb.SessionMDB">
        <property name="feedPropertiesDTO" ref="feedListenerMarketMaker1Properties" />

    </bean>

But I need my application to create the MDB instances programmaticly.

I mean i want to create the mdb's via the code since each MDB will have different validation values for the messages that it will retrieve from the topic(via the feedPropertiesDTO)

basically I will have pool of MDB's with the same logic but each one will have different properties. the creation time of the MDB'S must be on runtime.

is that possible?

thanks, ray.


Solution

  • I think you can use factory method for instantating your MDB bean and use method

    Object getBean(String name, Object... args) throws BeansException;
    

    of the ApplicationContext in your code to instantiate bean frogramatically.

    As I know this method allows you to path argument to factory method.

    Here what is said in java doc for this method:

    Return an instance, which may be shared or independent, of the specified bean.

    Allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition.

    I have never used this approach but I think it colud work for your case.

    EDIT.

    Here is an example that demonstrates about what I'm talkin (It is very simple but I don't have enough time to write more complicated).

    Suppose that have interface and two it's implementations:

    public interface StringMakerInterface {
    
        // Just return simple String depending on concrete implementation.
        String returnDummyString();
    
    }
    
    public class StringMakerImpl1 implements StringMakerInterface {
    
        public String returnDummyString() {
            return "test bean impl 1";
        }
    
    
    }
    
    public class StringMakerImpl2 implements StringMakerInterface{
    
        public String returnDummyString() {
            return "test bean impl 2";
        }
    
    }
    

    And we have one class that uses concrete implementation of this interface and to which we should dinamically inject conctrete implementation:

    public class StringPrinter {
    
        private StringMakerInterface stringMaker;
    
        public StringMakerInterface getStringMaker() {
            return stringMaker;
        }
    
        public void setStringMaker(StringMakerInterface stringMaker) {
            this.stringMaker = stringMaker;
        }
    
        public StringPrinter() {
    
        }
    
        // Just print dummy string, returned by implementation
        public void printString() {
            System.out.println(stringMaker.returnDummyString());
        }
    }
    

    Here is configuration class for my example:

    @Configuration
    public class TestFactoryMethodConfig {
    
        @Bean(name = "stringMaker1")
        public StringMakerImpl1 stringMaker1() {
            return new StringMakerImpl1();
        }
    
        @Bean(name = "stringMaker2")
        public StringMakerImpl2 stringMaker2() {
            return new StringMakerImpl2();
        }
    
        @Bean(name = "stringPrinter")
        @Scope(value = "prototype")
        public StringPrinter stringPrinter(@Qualifier("stringMaker1") StringMakerInterface stringMaker) {
            StringPrinter instance = new StringPrinter();
            instance.setStringMaker(stringMaker);
    
            return instance;
        }
    }
    

    And here is a test case that demonstrates dinamically injection at the runtime:

    @RunWith(value = SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes={TestFactoryMethodConfig.class})
    public class TestFactoryMethod {
    
        @Autowired
        private ApplicationContext applicationContext;
    
        @Resource(name = "stringMaker1")
        private StringMakerInterface stringMaker1;
    
        @Resource(name = "stringMaker2")
        private StringMakerInterface stringMaker2;
    
        @Test
        public void testFactoryMethodUsage() {
    
            StringPrinter stringPrinter1 = (StringPrinter) applicationContext.getBean("stringPrinter", stringMaker1);
    
            StringPrinter stringPrinter2 = (StringPrinter) applicationContext.getBean("stringPrinter", stringMaker2);
    
            stringPrinter1.printString();
            stringPrinter2.printString();
        }
    
    }