在Spring Boot应用程序中需要wso2mb进行消息传递。如何在Spring Boot中建立连接?
需求是要有一个侦听器,该侦听器正在侦听JMS队列并相应地处理数据。有没有一种方法可以在Spring Boot应用程序中完成此任务。尝试过https://docs.wso2.com/display/MB300/Sending+and+Receiving+Messages+Using+Queues中提供的基于Java的示例方法,效果很好。
包括JARS:
andes-client-3.2.19.jar
geronimo-jms_1.1_spec-1.1.0.wso2v1.jar
org.wso2.securevault-1.0.0-wso2v2.jar
编译组:“ org.springframework”,名称:“ spring-jms”,版本:“ 4.1.0.RELEASE”
springBootVersion ='2.0.4.RELEASE'
Application.java
@EnableJms
@SpringBootApplication
@ServletComponentScan
public class Application extends SpringBootServletInitializer {
public static final String QPID_ICF = "org.wso2.andes.jndi.PropertiesFileInitialContextFactory";
private static final String CF_NAME_PREFIX = "connectionfactory.";
private static final String CF_NAME = "qpidConnectionfactory";
String userName = "admin";
String password = "admin";
private static String CARBON_CLIENT_ID = "carbon";
private static String CARBON_VIRTUAL_HOST_NAME = "carbon";
private static String CARBON_DEFAULT_HOSTNAME = "localhost";
private static String CARBON_DEFAULT_PORT = "5673";
String queueName = "testQueue";
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
context.getBean(JmsTemplate.class);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
@Bean
public JmsListenerContainerFactory<?> myFactory
(QueueConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer, QueueConnection connection) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
}
@Bean
public QueueConnection myConnection
(QueueConnectionFactory factory) throws JMSException {
QueueConnection queueConnection = factory.createQueueConnection();
queueConnection.start();
return queueConnection;
}
@Bean
public QueueConnectionFactory queueConnectionFactory() throws NamingException {
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF);
properties.put(CF_NAME_PREFIX + CF_NAME, getTCPConnectionURL(userName, password));
properties.put("queue."+ queueName,queueName);
InitialContext ctx = new InitialContext(properties);
QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("qpidConnectionfactory");
return connFactory;
}
private String getTCPConnectionURL(String username, String password) {
// amqp://{username}:{password}@carbon/carbon?brokerlist='tcp://{hostname}:{port}'
return new StringBuffer()
.append("amqp://").append(username).append(":").append(password)
.append("@").append(CARBON_CLIENT_ID)
.append("/").append(CARBON_VIRTUAL_HOST_NAME)
.append("?brokerlist='tcp://").append(CARBON_DEFAULT_HOSTNAME).append(":").append(CARBON_DEFAULT_PORT).append("'")
.toString();
}
@Bean
public MappingJackson2MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
}
实际结果: 启动Spring Boot应用程序时出现运行时异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myFactory' defined in com.ext.gr.Application: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.jms.config.JmsListenerContainerFactory]: Factory method 'myFactory' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.jms.config.DefaultJmsListenerContainerFactory.setAutoStartup