简体   繁体   English

Weblogic和Spring JMS模板中,只有一个连接工厂的同一个模块中的多个队列

[英]Multiple queues in same Module with only one connection factory in weblogic and spring JMS template

I'm new with JMS and queues. 我是JMS和队列的新手。 Please excuse my knowledge. 请原谅我。

Server : Weblogic 12.x Frameworks: Spring, Hibernate, Spring JMS with JMSTemplate. 服务器:Weblogic 12.x框架:Spring,Hibernate,带有JMSTemplate的Spring JMS。

I have a business case where a scheduler wakes up for every X mins and processes the records and pushes them into queue. 我有一个业务案例,其中调度程序每X分钟醒来一次,并处理记录并将其推入队列。 In fact, we have 2 schedulers doing the same but for diff tables. 实际上,我们有2个调度程序执行相同的操作,但只对diff表执行。 So, for this case, I have implemented a no xml approach(fully annotated) for JMS template. 因此,对于这种情况,我为JMS模板实现了一个无xml方法(完全注释)。 I have setup a single module in weblogic with a connection factory and 2 queues. 我在weblogic中设置了一个带有连接工厂和2个队列的模块。 I have tested it and its working fine. 我已经对其进行了测试,并且工作正常。

However, 然而,

  1. Is this a good approach to use 2 queues with a single conn factory? 这是一个在单个conn工厂中使用2个队列的好方法吗?
  2. In the QueueMessageSender, I am using the below annotations to send the message. 在QueueMessageSender中,我使用以下注释发送消息。

      @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! 关于这个主题的任何建议都可能对我有帮助。您可以通过示例为基础的方法为Spring建议的JMS书籍中的任何一本都不错!

Here is the config file in java: 这是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();
    }

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. 由于它们都使用相同的连接工厂,因此可以使用单个JmsTemplate并在每次发送时传递目的地,但是没有令人信服的理由只使用两个队列。 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. ...因为该字符串必须转换为JMS消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM