我在我的项目中使用Spring jms模块。我的目标是创建一个可由其他应用程序触发的可执行jar文件。我在weblogic服务器中配置jms,所以,我不打算使用Spring-Boot解决方案
@Configuration
@ComponentScan(basePackages = "com.myapp")
@EnableJms
public class MyApp {
public static void main(String[] args) {
ApplicationContext context = null;
try {
context = new AnnotationConfigApplicationContext(MyApp.class);
MyBeanProvider beanProvider = context.getBean(MyBeanProvider.class);
System.out.println(beanProvider);
} catch (BeansException e) {
e.printStackTrace();
} finally{
((AbstractApplicationContext)context).close();
}
}
}
@Component
public class MyBeanProvider {
@Bean
public JndiTemplate getJndiTemplate() {
JndiTemplate jndiTemplate = new JndiTemplate();
Properties properties = new Properties();
properties.put("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
properties.put("java.naming.provider.url", "t3://localhost:7001");
jndiTemplate.setEnvironment(properties);
return jndiTemplate;
}
@Bean(value="MyBeanConnectionFactory")
public JndiObjectFactoryBean getConnectionFactory() {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiTemplate(getJndiTemplate());
jndiObjectFactoryBean.setJndiName("jms/MyAppConnectionFactory");
return jndiObjectFactoryBean;
}
@Bean
public JndiDestinationResolver getDestinationResolver() {
JndiDestinationResolver jndiDestinationResolver = new JndiDestinationResolver();
jndiDestinationResolver.setCache(true);
jndiDestinationResolver.setJndiTemplate(getJndiTemplate());
return jndiDestinationResolver;
}
@Bean
public JmsTemplate getJmsTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory
((javax.jms.ConnectionFactory)getConnectionFactory().getObject());
jmsTemplate.setDestinationResolver(getDestinationResolver());
return jmsTemplate;
}
}
}
我有一个消息接收器类如下:
@Component
public class MyBeanMessageReceiver {
@JmsListener(destination = "jms/queue/MyAppMessageQueue",containerFactory="MyBeanConnectionFactory")
public void receiveMessage(String message) {
System.out.println("Received <" + message + ">");
}
}
在执行期间,我遇到了以下异常跟踪:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyBeanMessageReceiver' defined in file : Initialization of bean failed; nested exception is
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'MyBeanConnectionFactory' is expected to be of type
'org.springframework.jms.config.JmsListenerContainerFactory' but was actually of type 'weblogic.jms.client.JMSXAConnectionFactory'
答案 0 :(得分:0)
containerFactory="MyBeanConnectionFactory"
containerFactory
媒体资源必须引用JmsListenerContainerFactory
而不是ConnectionFactory
。
反过来,容器工厂会获得对连接工厂的引用。
请参阅documentation。