如何在Spring启动应用程序中覆盖JMS connectionfactory和队列

时间:2017-12-30 01:03:52

标签: java spring jms jndi

我是Spring Boot和JMS的新手,并尝试使用简单的Producer类进行设置,以便将消息发送到队列。我想创建一个可以在ActiveMQ和Weblogic上工作的泛型类。 在使用ActiveMQ进行测试时,如果我使用'ConnectionFactory'进行查找并进行动态排序,则可以正常工作。 但是当我尝试从属性文件中进行查找时,它会给我一个'NameNotFound异常'。

队列和连接工厂已存在于weblogic以及activemq。

下面是代码ApplicationConfig.java //设置bean实例化

        @Configuration
        @EnableJMS
        public class AppConfiguration{

        @Autowired
        private JMSProperties jmsProps;

        @Bean
        public InitialContex getInitialContext() throws Exception{
        InitialContext ctx = null;
        try{
        ctx = new InitialContext();}
        catch(NamingException ne){
        throw ne;
        }
        }
        return ctx;
        }

        @Bean
        public ConnectionFactory getconnFac(InitialContext ctx) throws Exception{
        ConnectionFactory cf = null;
        try{
        cf = (ConnectionFactory) ctx.lookup(jmsProps.getConnectionFactoryJndi());
        }
        catch(NamingException ne){
        throw ne;
        }
        return cf;
        }


        @Bean
        public Destination getDest(InitialContext ctx) throws Exception {
        Destination dest = null;
        try{
        dest = (Destination) ctx.loolup (jmsProps.getTargetQueueJndi());
        }
        catch(NamingException ne){
        throw ne;
        }
        return dest;
        }


        @Bean
        public JmsTemplate getJmsTemplate(ConnectionFactory cf){
        JmsTemplate jmsTemplate = new JmsTemplate(cf);
        return jmsTemplate;
        }
        }

JmsProperties.java //用于读取jms属性的文件

        @Component
        @ConfigurationProperties
        @PropertySouce("classpath:/jms.properties")
        public class JmsProperties{
        private String targetQueueJndi;
        private String connectionFactoryJndi;

        public String getTargetQueueJndi(){
        return targetQueueJndi;
        }

        public void setTargetQueueJndi(String targetQueueJndi){
        this.targetQueueJndi = targetQueueJndi;
        }

        public String getConnectionFactoryJndi(){
        return connectionFactoryJndi;
        }

        public void setConnectionFactoryJndi(String connectionFactoryJndi){
        this.connectionFactoryJndi = connectionFactoryJndi;
        }

        }

jms.properties //用于查找实际连接工厂和队列的文件

        connection-factory.name = MQOOFDEACF
        connection-factory.local-jndi = jms/MQOOFDEACF

        target-queue.name = MQOOFDEAQ
        target-queue.local-jndi = jms/MQOOFDEAQ


        application-loc.properties

        target-queue-jndi = ${target-queue.local-jndi}
        connection-factory-jndi=${connection-factory.local-jndi}

抛出异常://构建

时抛出异常
caused by: org.springframework.beans.BeanInstantiationException: Failed to instatiate [javax.jms.ConnectionFactory]: Factory method 'getConnFac' threw exception; nested excetion is javax.naming.NameNotFoundException: jms/MQOOFDEACF

0 个答案:

没有答案