У меня есть приложение Spring Boot 5, и оно также работает на одном сервере IBM MQ.
Теперь мы хотим, чтобы он подключался к трем или более серверам MQ. Теперь я собираюсь добавить информацию о подключении XY в среду, а затем я получу бины XY MQConnectionFactory и все остальные бины, необходимые для обработки.
На данный момент у меня есть следующее:
@Bean
@Qualifier(value="MQConnection")
public MQConnectionFactory getIbmConnectionFactory() throws JMSException {
MQConnectionFactory factory = new MQConnectionFactory();
// seeting all the parameters here
return factory;
}
Но это довольно устоявшееся c. Есть ли элегантный способ сделать это?
Я наткнулся на IntegrationFlow. Это возможно рабочее решение?
Спасибо за все ваши советы!
KR
Решение
Основано на ответе Артема Билана Я построил этот класс.
@Configuration
public class ConnectionWithIntegrationFlowMulti {
protected static final Logger LOG = Logger.create();
@Value("${mq.queue.jms.sources.queue.queue-manager}")
private String queueManager;
@Autowired
private ConnectionConfig connectionConfig;
@Autowired
private SSLSocketFactory sslSocketFactory;
@Bean
public MessageChannel queureader() {
return new DirectChannel();
}
@Autowired
private IntegrationFlowContext flowContext;
@PostConstruct
public void processBeanDefinitionRegistry() throws BeansException {
Assert.notEmpty(connectionConfig.getTab().getLocations(), "At least one CCDT file locations must be provided.");
for (String tabLocation : connectionConfig.getTab().getLocations()) {
try {
IntegrationFlowRegistration theFlow = this.flowContext.registration(createFlow(tabLocation)).register();
LOG.info("Registered bean flow for %s with id = %s", queueManager, theFlow.getId());
} catch (JMSException e) {
LOG.error(e);
}
}
}
public IntegrationFlow createFlow(String tabLocation) throws JMSException {
LOG.info("creating ibmInbound");
return IntegrationFlows.from(Jms.messageDrivenChannelAdapter(getConnection(tabLocation)).destination(createDestinationBean()))
.handle(m -> LOG.info("received payload: " + m.getPayload().toString()))
.get();
}
public MQConnectionFactory getConnection(String tabLocation) throws JMSException {
MQConnectionFactory factory = new MQConnectionFactory();
// doing stuff
return factory;
}
@Bean
public MQQueue createDestinationBean() {
LOG.info("creating destination bean");
MQQueue queue = new MQQueue();
try {
queue.setBaseQueueManagerName(queueManager);
queue.setBaseQueueName(queueName);
} catch (Exception e) {
LOG.error(e, "destination bean: Error for integration flow");
}
return queue;
}
}