我认为你不需要改变myFactory方法本身,而是需要创建connectionFactory,它连接到Oracle队列。我有类似的配置,在开发中我使用artemis来运行我的JUNIT,在生产环境中我使用Oracle队列。下面是我定义的创建connectionFactory的类。
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.naming.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.springframework.jndi.JndiTemplate;
@Configuration
@EnableJms
@ConfigurationProperties(prefix = "spring.wls.jms")
@ConditionalOnProperty(prefix = "spring.wls.jms", name = "url")
public class WLSJmsConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(WLSJmsConfiguration.class);
private String url;
private String username;
private String password;
private String connectionFactoryName;
private String targetQueue;
private String replyQueue;
public void setUrl(final String url) {
this.url = url;
}
public void setUsername(final String username) {
this.username = username;
}
public void setPassword(final String password) {
this.password = password;
}
public void setConnectionFactoryName(final String connectionFactoryName) {
this.connectionFactoryName = connectionFactoryName;
}
public void setTargetQueue(final String targetQueue) {
this.targetQueue = targetQueue;
}
public void setReplyQueue(final String replyQueue) {
this.replyQueue = replyQueue;
}
private Properties getJNDiProperties() {
final Properties jndiProps = new Properties();
LOG.debug("Initializing JndiTemplate");
LOG.debug("Url is {}", url);
jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
jndiProps.setProperty(Context.PROVIDER_URL, url);
if (username != null && !username.isEmpty()) {
jndiProps.setProperty(Context.SECURITY_PRINCIPAL, username);
}
if (password != null && !password.isEmpty()) {
jndiProps.setProperty(Context.SECURITY_CREDENTIALS, password);
}
return jndiProps;
}
@Bean
public JndiTemplate jndiTemplate() {
final JndiTemplate jndiTemplate = new JndiTemplate();
jndiTemplate.setEnvironment(getJNDiProperties());
return jndiTemplate;
}
@Bean(name = "jmsJndiConnectionFactory")
public JndiObjectFactoryBean jndiObjectFactoryBean(final JndiTemplate jndiTemplate) {
final JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
LOG.debug("Creating Weblogic JMS connection factory");
jndiObjectFactoryBean.setJndiTemplate(jndiTemplate);
LOG.debug("ConnectoinFactory Name is {}", connectionFactoryName);
jndiObjectFactoryBean.setJndiName(connectionFactoryName);
return jndiObjectFactoryBean;
}
@Bean(name = "jmsWlsConnectionFactory")
public ConnectionFactory jmsConnectionFactory(final JndiObjectFactoryBean jndiObjectFactoryBean) {
final ConnectionFactory connectionFactory = (ConnectionFactory) jndiObjectFactoryBean.getObject();
LOG.debug("ConnectoinFactory is null? {}", connectionFactory == null);
return connectionFactory;
}
@Bean(name = "jmsConnectionFactory")
@Primary
public ConnectionFactory connectionFactoryProxy() {
final CachingConnectionFactory jmsConnectionFactory = new CachingConnectionFactory(
(ConnectionFactory) appContext.getBean("jmsWlsConnectionFactory"));
jmsConnectionFactory.setCacheProducers(true);
jmsConnectionFactory.setSessionCacheSize(20);
return jmsConnectionFactory;
}
@Bean
public Destination jmsQueueName() {
final JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiTemplate(jndiTemplate());
jndiObjectFactoryBean.setJndiName(targetQueue);
return (Destination) jndiObjectFactoryBean.getObject();
}
private DestinationResolver destinationResolver() {
final JMSDestinationResolver destinationResolver = new JMSDestinationResolver();
final JndiHelper jndiHelper = new JndiHelper(getJNDiProperties());
destinationResolver.setJndiTemplate(jndiHelper);
return destinationResolver;
}
}
应用程序属性文件。
spring.wls.jms.url=t3://server01:8001,server02:8003
spring.wls.jms.username=weblogic
spring.wls.jms.password=password
spring.wls.jms.connectionFactoryName=connectionFactory Name
spring.wls.jms.targetQueue=queue_name
spring.wls.jms.replyQueue=queue_name
您需要将wlthint3client
添加到您的类路径中。我从 <weblogic_home>\wlserver\server\lib
获取了该 jar 包,并创建了一个 maven 依赖项来使用该 jar 包并推送到我的本地仓库,然后将其作为依赖项添加到项目中。
<dependency>
<groupId>com.oracle.weblogic</groupId>
<artifactId>wlthint3client</artifactId>
<version>12.2.1</version>
<scope>provided</scope> <!
</dependency>
qconFactory = AQjmsFactory.getQueueConnectionFactory(hostName, serviceName, port, "thin"); qcon = qconFactory.createQueueConnection(userName, password); qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queueTable = ((AQjmsSession) qsession).getQueueTable(userName, queueTableName); queue = ((AQjmsSession) qsession).getQueue(userName, queueName);
- dhmc