0

How to create a JMS messagedriven adapter to connect to Oracle weblogic JMS queue using Spring integration with Java Integration DSL.

2
  • 2
    This is not a question. Please check how-to-ask Commented Jul 4, 2018 at 6:42
  • Thanks MarkusEgle !! Commented Jul 4, 2018 at 7:16

1 Answer 1

1

You can use the below code to connect to Weblogic JMS queue using Spring integrtion with Java dsl configuration.

  1. Firstly , We need to create a connection factory & Destination Resolver object which has to be passed to Jms messageDrivenChannelAdapter

    Below code is used to create a 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;
        }
    }
    

    1. In your main class of spring boot add the below code :

    `

    @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.Add this properties in your application.properties

    spring.jms.username= Username spring.jms.password= Password spring.jms.queue= Queue name spring.jms.url= Weblogic server url spring.jms.connectionFactoryName= Connectionfactory name ex jms/TestConnectionFactory


    1. Make sure to add the wlthint3client.jar oracle jar in your pom.xml.

    Hope this helps you ALL!!!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.