Используя весеннюю загрузку и IBM MQ, мне нужно отправить сообщение в MQ.
В загрузочном приложении My 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.
Разве Spring не должен пытаться вызвать DestinationResolver?
Если нет, как мне зарегистрировать пункт назначения для IBM MQ?
Вы определяете компонент DestinationResolver
, но вводите компонент MQDestination
. Вот почему вы получаете ошибку. Вы должны ввести DestinationResolver
и вызвать setDestinationResolver
на JmsTemplate
.
Но JmsTemplate
динамически разрешает адресаты. Это также работает:
public void sendMessage(String textMessage) {
String destination = "MY.QUEUE";
jmsTemplate.convertAndSend(destination, textMessage);
}