SpringBoot整合IBMMQ:JMS监听、收发消息,以及配置多个队列管理器通信

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37497666/article/details/88551735

1、配置mq参数:.yml文件或是类似config配置文件

test:
	host:127.0.0.1
	port:1414
	username:test
	password:test 
	channel:TEST_CHANNEL   //通道
	queue:TEST_QUEUE      //队列名
	queue.manager:MANAGER   //队列管理器
	ccsid:1381  //字符集 1381 默认字符主要用于windows环境  1208 --utf-8
	receive-timeout:20000   //超时时间(接收是阻塞式的接收,默认会一直阻塞等待,直到接收到了消息。设置一个最长的等待时间参数,超过这个时间,接收的方法将得到null的结果。阻塞的时候,程序被挂起,挂起也就意味着程序卡在了这里,这时引入了超时等待,释放资源)

2、创建jms配置类:

import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.wmq.WMQConstants;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter;
import org.springframework.jms.core.JmsOperations;
import org.springframework.jms.core.JmsTemplate;


@Configuration
public class TestCreator {

    private static String host;

    private static Integer port;

    private static String queueManager;

    private static String channel;

    private static String username;

    private static String password;

    private static int ccsid;

    private static String queueName;

    private static long receiveTimeout;

    @Value("${test.host}")
    public void setHost(String host) {
        TestCreator.host = host;
    }
    @Value("${test.port}")
    public void setPort(Integer port) {
        TestCreator.port = port;
    }
    @Value("${test.queue.manager}")
    public void setQueueManager(String queueManager) {
        TestCreator.queueManager = queueManager;
    }
    @Value("${test.channel}")
    public void setChannel(String channel) {
        TestCreator.channel = channel;
    }
    @Value("${test.username}")
    public void setUsername(String username) {
        TestCreator.username = username;
    }
    @Value("${test.password}")
    public void setPassword(String password) {
        TestCreator.password = password;
    }
    @Value("${test.ccsid}")
    public void setCcsid(int ccsid) {
        TestCreator.ccsid = ccsid;
    }
    @Value("${test.queue}")
    public void setQueueName(String queueName) {
        TestCreator.queueName = queueName;
    }
    @Value("${test.receive-timeout}")
    public void setReceiveTimeout(long receiveTimeout) {
        TestCreator.receiveTimeout = receiveTimeout;
    }
	//配置连接工厂
    @Bean
    public MQQueueConnectionFactory mqQueueConnectionFactory() {
        MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
        mqQueueConnectionFactory.setHostName(host);
        try {
            mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
            mqQueueConnectionFactory.setCCSID(ccsid);
            mqQueueConnectionFactory.setChannel(channel);
            mqQueueConnectionFactory.setPort(port);
            mqQueueConnectionFactory.setQueueManager(queueManager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mqQueueConnectionFactory;
    }
	//配置连接认证 如不需要账户密码链接可以跳过此步,直接将mqQueueConnectionFactory注入下一步的缓存连接工厂。
	@Bean
    UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(MQQueueConnectionFactory mqQueueConnectionFactory) {
        UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
        userCredentialsConnectionFactoryAdapter.setUsername(username);
        userCredentialsConnectionFactoryAdapter.setPassword(password);
        userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqQueueConnectionFactory);
        return userCredentialsConnectionFactoryAdapter;
    }
	//配置缓存连接工厂 不配置该类则每次与MQ交互都需要重新创建连接,大幅降低速度。
    @Bean
    @Primary
    public CachingConnectionFactory cachingConnectionFactory(UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter) {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setTargetConnectionFactory(userCredentialsConnectionFactoryAdapter);
        cachingConnectionFactory.setSessionCacheSize(500);
        cachingConnectionFactory.setReconnectOnException(true);
        return cachingConnectionFactory;
    }
	//配置DefaultJmsListenerContainerFactory, 用@JmsListener注解来监听队列消息时候,尤其存在多个监听的时候,通过实例化配置DefaultJmsListenerContainerFactory来控制消息分发
    @Bean(name = "jmsQueueListenerCF")
    public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory(CachingConnectionFactory cachingConnectionFactory) {
        DefaultJmsListenerContainerFactory factory =
                new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(cachingConnectionFactory);
        //设置连接数
        factory.setConcurrency("3-10");
        //重连间隔时间
        factory.setRecoveryInterval(1000L);
        return factory;
    }
	//配置JMS模板,实例化jmsOperations后,可以在方法中通过autowired的方式注入模板,用方法调用发送/接收消息
	(如果只是接收消息,可以不配置此步)
    @Bean
    public JmsOperations jmsOperations(CachingConnectionFactory cachingConnectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory);
        jmsTemplate.setReceiveTimeout(receiveTimeout);//设置超时时间
        return jmsTemplate;
    }
}

3、创建监听接收消息

@Slf4j
@Component
public class MdxpMessageListener extends MessageListenerAdapter {//此处继承MessageListenerAdapter,此处若只使用@JmsListener注解监听,会出现监听消息获取不及时或者获取不到消息的情况,加上MessageListenerAdapter后便不会出现
    @Override  // 此处destination必须使用静态变量,暂时没找到替换为动态变量的方法,containerFactory用来给监听器指定配置源
    @JmsListener(destination = "TEST_QUEUE",containerFactory = "jmsQueueListenerCF")
    public void onMessage(Message message) {
        String msgStr ="";
        try {  //JmsListener收到消息后,会自动封装成自己特有的数据格式,可以用TextMessage来解析原始消息
            TextMessage msg = (TextMessage) message;
            msgStr = msg.getText();
        } catch (JMSException e) {
            log.error("消息接收异常!", e);
        }
    }
}

5、配置所需jar

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
</dependency>
<dependency>
    <groupId>javax.jms</groupId>
    <artifactId>javax.jms-api</artifactId>
    <version>2.0.1</version>
</dependency>

 

展开阅读全文

没有更多推荐了,返回首页