IBM-MQ创建MQ目标

时间:2019-04-08 06:23:59

标签: spring-boot ibm-mq

使用spring boot和IBM MQ,我需要将消息发送到MQ。

在我的Spring引导应用程序中,我注册了MQQueueConnectionFactory,如下所示。

@SpringBootApplication
@EnableJms
public class MainApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(MainApplication.class).web(WebApplicationType.NONE).run(args);
        logger.info("init completed...");
    }

    @Bean
    public MQQueueConnectionFactory queueConnectionFactory() {
        MQQueueConnectionFactory queueConnectionFactory = new MQQueueConnectionFactory();
        try {
            queueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
            queueConnectionFactory.setHostName(host);
            queueConnectionFactory.setChannel(channel);
            queueConnectionFactory.setPort(port);
            queueConnectionFactory.setQueueManager(queueManager);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return queueConnectionFactory;
    }

}

我有如下的目标解析器。

 @Component
 public class IBMWebSphereMqDestinationResolver extends 
  DynamicDestinationResolver implements DestinationResolver {

@Override
public Destination resolveDestinationName(Session session, String destinationName, boolean pubSubDomain) throws JMSException {
    Destination destination = super.resolveDestinationName(session, destinationName, pubSubDomain);
    if (destination instanceof MQDestination) {
        MQDestination mqDestination = (MQDestination) destination;
    }
    return destination;
}

}

我正在使用JmsTemplate将消息发送到MQ。

@Service
public class MqServiceImpl implements MqService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private MQDestination destination;

    @Handler
    @Override
    public void sendMessage(String textMessage) {
        logger.info("textMessage {} ", textMessage);
        logger.info("destination {}  ", destination);
        jmsTemplate.convertAndSend(destination, textMessage);
    }

}

但是,当我尝试启动应用程序时,却得到MQDestination' that could not be found.

service.impl.MqServiceImpl required a bean of type 'com.ibm.mq.jms.MQDestination' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.ibm.mq.jms.MQDestination' in your configuration.

春天不应该尝试调用DestinationResolver吗?

如果没有,我如何为IBM MQ注册目的地?

1 个答案:

答案 0 :(得分:1)

您要定义一个DestinationResolver bean,但要注入一个MQDestination bean。这就是为什么您得到错误。您应该注入DestinationResolver并在setDestinationResolver上调用JmsTemplate

但是JmsTemplate动态地解析目的地。这也可以:

public void sendMessage(String textMessage) {
    String destination = "MY.QUEUE";
    jmsTemplate.convertAndSend(destination, textMessage);
}