gpt4 book ai didi

java - 使用 Spring 与 Java DSL 配置集成连接到 Weblogic JMS 队列

转载 作者:行者123 更新时间:2023-12-02 11:03:23 45 4
gpt4 key购买 nike

如何使用 Spring 与 Java Integration DSL 集成创建 JMS 消息驱动适配器以连接到 Oracle weblogic JMS 队列。

最佳答案

您可以使用以下代码通过 Spring 集成与 Java dsl 配置连接到 Weblogic JMS 队列。

  1. 首先,我们需要创建一个连接工厂和 Destination Resolver 对象,该对象必须传递给 Jms messageDrivenChannelAdapter

    下面的代码用于创建一个connectionFactory:

           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;
    }
    }
    <小时/>
  2. 在 Spring Boot 的主类中添加以下代码:

`

@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.将此属性添加到您的 application.properties

spring.jms.username=用户名spring.jms.password=密码spring.jms.queue=队列名称spring.jms.url=Weblogic服务器urlspring.jms.connectionFactoryName= 连接工厂名称 ex jms/TestConnectionFactory

<小时/>
  • 确保在 pom.xml 中添加 wlthint3client.jar oracle jar。
  • 希望这对大家有帮助!!!

    关于java - 使用 Spring 与 Java DSL 配置集成连接到 Weblogic JMS 队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51167260/

    45 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com