0
votes

I'm new with JMS and queues. Please excuse my knowledge.

Server : Weblogic 12.x Frameworks: Spring, Hibernate, Spring JMS with JMSTemplate.

I have a business case where a scheduler wakes up for every X mins and processes the records and pushes them into queue. In fact, we have 2 schedulers doing the same but for diff tables. So, for this case, I have implemented a no xml approach(fully annotated) for JMS template. I have setup a single module in weblogic with a connection factory and 2 queues. I have tested it and its working fine.

However,

  1. Is this a good approach to use 2 queues with a single conn factory?
  2. In the QueueMessageSender, I am using the below annotations to send the message.

        @Autowired
            private JmsTemplate jmsTemplate_Queue_1;
            @Autowired
            private JmsTemplate jmsTemplate_Queue_2;
    
            jmsTemplate_Queue_1.send(wrapMessage("Hello"));
            jmsTemplate_Queue_1.send(wrapMessage("test"));
    
    private MessageCreator wrapMessage(final String msg) {
            return new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    ObjectMessage om = session.createObjectMessage();
                    om.setObject(msg);                
                    return om;
                }
            };
    }
    

Is this the right way to do it? In future what possible problems can I run into if this is not a right approach.

Any suggestions on this topic could be helpful to me.Any good books you can suggest for JMS with spring with an example based approach would be great!

Here is the config file in java:

@Configuration
@EnableJms
@ComponentScan({ "com.xxxx.xxx.config" })
@PropertySource("classpath:application.properties")
public class JmsConfiguration{

    @Autowired
    private Environment environment;

    @Bean
    public JndiTemplate jndiTemplate() {
        JndiTemplate jndiTemplate = new JndiTemplate();
        Properties jndiProps = new Properties();

        jndiProps.setProperty("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
        Properties props = System.getProperties() ;
        jndiProps.setProperty("java.naming.provider.url","t3://localhost:7001"); 
        jndiTemplate.setEnvironment(jndiProps);
        return jndiTemplate;
    }

    @Bean
    public JndiObjectFactoryBean jmsConnectionFactory() {
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();

        jndiObjectFactoryBean.setJndiTemplate(jndiTemplate());
        jndiObjectFactoryBean.setJndiName(environment.getProperty("jms.connectionFactory"));

        return jndiObjectFactoryBean;
    }

    @Bean
    public JndiObjectFactoryBean queue_one() {
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();

        jndiObjectFactoryBean.setJndiTemplate(jndiTemplate());
        jndiObjectFactoryBean.setJndiName(environment.getProperty("jms.queue_one")); 
        return jndiObjectFactoryBean;
    }

    @Bean
    public JndiObjectFactoryBean queue_two() {
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();

        jndiObjectFactoryBean.setJndiTemplate(jndiTemplate());
        jndiObjectFactoryBean.setJndiName(environment.getProperty("queue_two")); 
        return jndiObjectFactoryBean;
    }

    @Bean
    public TransactionAwareConnectionFactoryProxy connectionFactoryProxy() {
        return new TransactionAwareConnectionFactoryProxy((ConnectionFactory) jmsConnectionFactory().getObject());
    }

    @Bean(name="jmsTemplate_Queue_1")
    public JmsTemplate jmsTemplate_Queue_1() {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactoryProxy());

        jmsTemplate.setSessionTransacted(false);
        jmsTemplate.setReceiveTimeout(5000);
        jmsTemplate.setDefaultDestination((Destination) queue_one().getObject());

        return jmsTemplate;
    }

    @Bean(name="jmsTemplate_Queue_2")
    public JmsTemplate jmsTemplate_Queue_2() {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactoryProxy());

        jmsTemplate.setSessionTransacted(false);
        jmsTemplate.setReceiveTimeout(5000);
        jmsTemplate.setDefaultDestination((Destination) queue_two().getObject());

        return jmsTemplate;
    }
    //Thank you for looking the code till here. I don't want to be rude by not saying thank you! 
    @Bean
    public QueueMessageSender queueMessageSender() {        
        return  new QueueMessageSender();
    }
1

1 Answers

0
votes

What you have is fine; since they both use the same connection factory, you could use a single JmsTemplate and pass in the destination on each send, but there's no compelling reason to do so with only two queues. If you had lots of queues, it might be easier to manage.

However, there is no such method...

jmsTemplate_Queue_1.send("Hello");

... I presume you mean

jmsTemplate_Queue_1.convertAndSend("Hello");

...since the String has to be converted to a JMS Message.