簡體   English   中英

將Spring的JndiObjectFactoryBean強制轉換為Solace-MQ JMS的ConnectionFactory時出錯

[英]Error Casting Spring's JndiObjectFactoryBean to ConnectionFactory for Solace-MQ JMS

我有一個很好的工作XML配置Camel Context,它使用JNDI和Spring

之后Solace.JndiObjectFactoryBean被用作connectionFactory

<bean id="Solace.JmsComponent" class="  on">
    <property name="connectionFactory" ref="Solace.JndiObjectFactoryBean" />
    <property name="destinationResolver" ref="Solace.JndiDestinationResolver" />
</bean>

我試圖將其轉換為從org.apache.camel.spring.javaconfig.CamelConfiguration擴展的Java類。 但是有一個問題。 當我嘗試在JMS組件component.setConnectionFactory(getJndiObjectFactoryBean())上設置連接工廠時; getJndiObjectFactoryBean(),我得到一個編譯時異常:

The method setConnectionFactory(ConnectionFactory) in the type JmsComponent 
is not applicable for the arguments (JndiObjectFactoryBean)

但是當我嘗試將從getJndiObjectFactoryBean返回的JndiObjectFactoryBean顯式地轉換為SolConnectionFactory時,我收到運行時錯誤

016-02-05 17:39:09,234|[localhost-startStop-1]|[]|[]|[ERROR] web.context.ContextLoader [line:307] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getJMSConfiguration' defined in class path resource [com//camel
/CamelRoutesConfig.class]: Instantiation of bean failed; nested exception is org
.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.apache.camel.component.jms.JmsConfiguration com.camel.CamelRoutesConfig.getJMSConfiguration()] threw exception; nested exception is java.lang.ClassCastException: org.springframework.jndi.JndiObjectFactoryBean$$EnhancerByCG
LIB$$18b94f95 cannot be cast to com.solacesystems.jms.SolConnectionFactory
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsi
ngFactoryMethod(ConstructorResolver.java:581)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1029)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
.createBeanInstance(AbstractAutowireCapableBeanFactory.java:925)

我相信在課堂上有必要的罐子。 sol-common-xyzjar,sol-jcsmp-xyzjar,sol-jms-xyzjar

JndiObjectFactoryBean無法轉換為ConnectionFactory

有兩種選擇:

  1. getJndiObjectFactoryBean()方法返回的JndiObjectFactoryBean中使用JndiObjectFactoryBean.getObject()
  2. 獲取Spring以提供ConnectionFactory

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); ConnectionFactory connectionFactory = (ConnectionFactory) context.getBean("Solace.JndiObjectFactoryBean");

當將一個有效的Spring XML配置(由Apache Camel用於從Weblogic JMS服務器隊列中讀取)移植到Spring-Boot偏好的Java配置時,我們遇到了類似的情況。

jmsConfiguration.setConnectionFactory( (javax.jms.ConnectionFactory)getJndiFactoryBean().getObject());

上面的代碼做了模仿的技巧

<bean id="jmsConfiguration" class="org.apache.camel.component.jms.JmsConfiguration">
    <property name="connectionFactory" ref="jndiFactoryBean"/>
    <property name="destinationResolver" ref="jndiDestinationResolver"/>
</bean>

(不清楚Spring XML在封面下做了多少額外的事情,因為它並不像在jndiFactoryBean上設置jndiFactoryBean那么jmsConfiguration

如果您使用getObject()方法,請確保在使用getObject()之前調用afterPropertiesSet()

org.springframework.jndi.JndiObjectFactoryBean cf = new org.springframework.jndi.JndiObjectFactoryBean();
JndiTemplate jndiTemplate = new org.springframework.jndi.JndiTemplate();
Properties environment = new Properties();
environment.setProperty("java.naming.factory.initial", "com.xxx"); //initial context
environment.setProperty("java.naming.provider.url", "xxx://xxx"); //url
jndiTemplate.setEnvironment(environment);
cf.setJndiTemplate(jndiTemplate);
cf.setJndiName("XXX");
//System.out.println(cf.getObject()); //null
cf.afterPropertiesSet();
//System.out.println(cf.getObject()); //now has the value

org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter adapter = new org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter();
adapter.setTargetConnectionFactory((ConnectionFactory) cf.getObject());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM