Code Monkey home page Code Monkey logo

ibm_websphere_mq_spring_boot_jms's Introduction

IBM WebSphere MQ integration with Spring Boot MQ (JavaConfig)

Maven Dependencies

Installcom.ibm.mq.allclient.jar

com.ibm.mq.allclient.jar is located at [MQ installed path]/java/lib/. Find it and install it to your local maven repository.

mvn install:install-file -Dfile=[jar path] -DgroupId=com.ibm.mq -DartifactId=allclient -Dversion=1.0 -Dpackaging=jar

Add Dependencies to the Project

  • Spring-activeMQ
  • IBM MQ Allclient
  • Javax JMS
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

<dependency>
    <groupId>com.ibm.mq</groupId>
    <artifactId>allclient</artifactId>
    <version>1.0</version>
</dependency>

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

JavaConfig

Configure MQ Rroperties and Read it in Project

application.yml

project: 
  mq:
    host: 192.168.1.180
    port: 1416
    queue-manager: QM
    channel: mqm.SVRCONN   # SVRCONN
    username: mqm
    password: 123456
    receive-timeout: 2000
@Configuration
public class JmsConfig {   
    @Value("${project.mq.host}")
    private String host;
    @Value("${project.mq.port}")
    private Integer port;
    @Value("${project.mq.queue-manager}")
    private String queueManager;
    @Value("${project.mq.channel}")
    private String channel;
    @Value("${project.mq.username}")
    private String username;
    @Value("${project.mq.password}")
    private String password;
    @Value("${project.mq.receive-timeout}")
    private long receiveTimeout;
}

Configure MQBMConnectionFactory

CCISD has to be the same within the Queue Manager, 1208 is UTF-8

@Bean
public MQQueueConnectionFactory mqQueueConnectionFactory() {
    MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
    mqQueueConnectionFactory.setHostName(host);
    try {
        mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
        mqQueueConnectionFactory.setCCSID(1208);
        mqQueueConnectionFactory.setChannel(channel);
        mqQueueConnectionFactory.setPort(port);
        mqQueueConnectionFactory.setQueueManager(queueManager);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mqQueueConnectionFactory;
}

Config 'UserCredentialsConnectionFactoryAdapter'

If you have to connect with Username and Password

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

Configure CachingConnectionFactory

Use @Primary annotation to tell Spring use this bean but not MQQueueConnectionFactory.

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

Configure JmsTransactionManager (Optional)

If you use transaction

@Bean
public PlatformTransactionManager jmsTransactionManager(CachingConnectionFactory cachingConnectionFactory) {
    JmsTransactionManager jmsTransactionManager = new JmsTransactionManager();
    jmsTransactionManager.setConnectionFactory(cachingConnectionFactory);
    return jmsTransactionManager;
}

Configure JmsOperations

Have to set Receive Timeout, or Receive Method would hang if queue is empty

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

Operation Example

Now you can inject jmsOperations to operate IBM WebSphere MQ Simply.

@Autowired
private JmsOperations jmsOperations;

Send Message to MQ

If you wanna send and Receive Object, that object have to implement Serializable Interface. Also Setting the SerialID is Better

@Autowired
JmsOperations jmsOperations;

public void send(User user){
  jmsOperations.convertAndSend("QUEUE.USER", user);
}

Receive Message to MQ

@Autowired
JmsOperations jmsOperations;
public void receive(User user){
  jmsOperations.receiveAndConvert("QUEUE.USER");
}

ibm_websphere_mq_spring_boot_jms's People

Watchers

Felipe Gutierrez avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.