JMS 的 Spring Tomcat 配置(IBM MQ、Tomcat、Spring)

标签 spring spring-boot tomcat ibm-mq mq

我有一个相对较旧的应用程序,它使用 Websphere MQ 进行消息传递。它在 WAS(Websphere 应用程序服务器)上运行并使用 MDB(消息驱动的 Bean)。我必须将该应用程序从 Websphere 迁移到 Tomcat

我尝试了一些使用 springboot 的东西,并且能够编写一个连接到队列和读取消息并能够处理它们的示例 JMS 应用程序,但尚未使用 JMS 实现事务管理。 现在我被要求配置应用程序以便它在 tomcat 上运行。 任何人都可以帮忙,我必须如何以及在何处设置 tomcat 中的配置。 或者如果将我的 springboot 应用程序打包为 war 并将其部署到 Tomcat 上,将需要进行哪些更改。

这就是我在 applicationconfig.java 中的代码的样子

@Bean(name = "mqQueueConnectionFactory")
    public MQQueueConnectionFactory mqQueueConnectionFactory() {
        MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
        try {
            mqQueueConnectionFactory.setHostName("hostname");
            mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
            mqQueueConnectionFactory.setCCSID(1208);
            mqQueueConnectionFactory.setChannel("channel");
            mqQueueConnectionFactory.setPort(1415);
            mqQueueConnectionFactory.setQueueManager("qManager");
        } catch (Exception e) {
            System.out.println("MQQueueConnectionFactory bean exception" + e);
        }
        return mqQueueConnectionFactory;
    }

    @Bean
    UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(
            MQQueueConnectionFactory mqQueueConnectionFactory) {
        UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
        userCredentialsConnectionFactoryAdapter.setUsername("");
        userCredentialsConnectionFactoryAdapter.setPassword("");
        userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqQueueConnectionFactory);
        return userCredentialsConnectionFactoryAdapter;
    }

    @Bean
    @Primary
    public CachingConnectionFactory cachingConnectionFactory(
            UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter) {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setTargetConnectionFactory(userCredentialsConnectionFactoryAdapter);
        cachingConnectionFactory.setReconnectOnException(true);
        return cachingConnectionFactory;
    }

    @Bean
    public JmsOperations jmsOperations(CachingConnectionFactory cachingConnectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory);
        jmsTemplate.setReceiveTimeout(50000);
        return jmsTemplate;
    }

最佳答案

@Bean(name = "wmq")
    public JmsComponent wmQ(@Value(AppConstants.WMQ_CONNECTION_TYPE) int connType,
                            @Value(AppConstants.WMQ_HOST) String hostName,
                            @Value(AppConstants.WMQ_PORT) Integer port,
                            @Value(AppConstants.WMQ_QUEUE_MANAGER) String queueManager,
                            @Value(AppConstants.WMQ_CHANNEL) String channel,
                            @Value(AppConstants.WMQ_CONCURRENT_CONSUMERS) int concurrentConsumers,
                            @Value(AppConstants.WMQ_USERNAME) String username,
                            @Value(AppConstants.WMQ_PASSWORD) String password
                           ) throws JMSException {
        JmsComponent jmsComponent = new JmsComponent();
        MQConnectionFactory mqConnectionFactory = new MQConnectionFactory();
        try {
            mqConnectionFactory.setTransportType(connType);
            mqConnectionFactory.setHostName(hostName);
            mqConnectionFactory.setPort(port);
            mqConnectionFactory.setQueueManager(queueManager);
            mqConnectionFactory.setChannel(channel);
            jmsComponent.setConnectionFactory(mqConnectionFactory);
            JmsConfiguration jmsConfiguration = new JmsConfiguration(mqConnectionFactory);
            jmsConfiguration.setUsername(username);
            jmsConfiguration.setPassword(password);
            jmsConfiguration.setConcurrentConsumers(concurrentConsumers);
            jmsComponent.setConfiguration(jmsConfiguration);
        } catch (JMSException e) {
            String msg = "Error while creating IBM MQ Connection Factory";
            throw new JMSException(msg);
        }
        return jmsComponent;
    }

关于JMS 的 Spring Tomcat 配置(IBM MQ、Tomcat、Spring),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56694259/

相关文章:

java - 如何在 Spring Boot 嵌入式 tomcat 中设置 HTTPS SSL Cipher Suite Preference

jsf - 如何验证 Jmeter 结果与手动结果

maven - tomcat 7 embedded 没有正确关闭 ClassNotFoundException ContainerBase$StopChild

tomcat - WsOutbound关闭函数导致Tomcat崩溃

java - 为 Spring 配置 Weblogic 11g

Spring 社交版本1.0.3 : Why i've Field or property 'name' cannot be found on null error

Spring Keycloak - 如何从 JWT 访问 token 设置主体

spring-boot - Spring Boot 执行器 "system.cpu.usage"与 "process.cpu.usage"

java - 在不向客户端公开方法的情况下更新 JMX MBean 属性

java - 尝试将 Spring 与 Jersey 一起使用时出现 NoSuchBeanDefinitionException