如何使用Spring与Java Integration DSL的集成创建JMS消息驱动的适配器以连接到Oracle weblogic JMS队列。
您可以使用以下代码使用 Spring 集成与 Java dsl 配置连接到 Weblogic JMS 队列。
-
首先,我们需要创建一个连接工厂和目的地解析器对象,该对象必须传递给 Jms messageDrivenChannelAdapter
下面的代码用于创建一个连接工厂:
import org.springframework.jms.listener.AbstractMessageListenerContainer; import org.springframework.jms.support.destination.DestinationResolver; import org.springframework.jms.support.destination.JndiDestinationResolver; import java.util.Properties; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.QueueConnectionFactory; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NameNotFoundException; import javax.naming.NamingException; import weblogic.jndi.WLInitialContextFactory; @Configuration @EnableJms public class JMSConfigurer { @Value("${spring.jms.url}") private String url; @Value("${spring.jms.username}") private String username; @Value("${spring.jms.password}") private String password; @Value("${spring.jms.connectionFactoryName}") private String connectionFactoryName; @Value("${spring.jms.queue}") private String mpiResponseQueue; private Properties getJNDiProperties() { final Properties jndiProps = new Properties(); 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; } /** * Create connection factory. * * @return */ @Bean public ConnectionFactory queueConnectionFactory() { // JNDI connection factory name stored in weblogic. return lookupByJndiTemplate(connectionFactoryName, QueueConnectionFactory.class); } /** * Create InitialContext. * * @return */ @Bean public JndiTemplate jndiTemplate() { JndiTemplate jndiTemplate = new JndiTemplate(); jndiTemplate.setEnvironment(getJNDiProperties()); return jndiTemplate; } @Bean public Destination mpiResponseQueue() { return lookupByJndiTemplate(mpiResponseQueue, Destination.class); } /** * * @param jndiName * @param requiredType * @return */ @SuppressWarnings("unchecked") protected <T> T lookupByJndiTemplate(String jndiName, Class<T> requiredType) { try { Object located = jndiTemplate().lookup(jndiName); if (located == null) { throw new NameNotFoundException("JNDI object with [" + jndiName + "] not found"); } return (T) located; } catch (NamingException e) { e.printStackTrace(); } return null; } /** * * @param jndiName * @param requiredType * @return */ @SuppressWarnings("unchecked") protected final <T> T lookup(String jndiName, Class<T> requiredType) { try { InitialContext initialContext = new InitialContext(getJNDiProperties()); Object located = initialContext.lookup(jndiName); if (located == null) { throw new NameNotFoundException("JNDI object with [" + jndiName + "] not found"); } return (T) located; } catch (NamingException e) { e.printStackTrace(); } return null; } }
- 在你的弹簧引导主类中添加以下代码:
'
@SpringBootApplication @IntegrationComponentScan public class JmsReaderApplication { @Autowired private javax.jms.ConnectionFactory queueConnectionFactory; @Autowired private Destination mpiResponseQueue; @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(JmsReaderApplication.class); } @Bean public IntegrationFlow jmsReader() { return IntegrationFlows .from(Jms.messageDrivenChannelAdapter(this.queueConnectionFactory) .destination(this.mpiResponseQueue)) .channel("queureader") .get(); } @ServiceActivator(inputChannel = "queureader") public void Print(Message<?> msg) { System.out.println(msg.getPayload().toString()); } }
3.在应用程序中添加此属性。
spring.jms.username= 用户名 spring.jms.password= 密码 spring.jms.queue= 队列名称 spring.jms.url= Weblogic server url spring.jms.connectionFactoryName= ConnectionFactory name ex jms/TestConnectionFactory
- 确保在你的pom.xml中添加wlthint3client.jar oracle jar。
希望这对您有所帮助!!