2017-04-17 14 views
7

Ich studiere den Frühling-Boot und JMS Beispiel und ja, ich bin ziemlich neu auf dieserWie Frühlings-Boot-JMS von ActiveMQ zu Oracle Advanced Queuing

Da wir arbeiten mit Oracle migrieren, würde ich migrieren möchten das Spring Boot & JMS-Beispiel von ActiveMQ zu Oracle Advanced Queuing. Jedoch finde ich wirklich sehr wenig Information darüber.

Soweit ich sehe, ich brauche den Code für die Oracle-Version zu ersetzen, aber ich habe es nicht geschafft zu herausgefunden, wie.

@Bean 
public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory, 
               DefaultJmsListenerContainerFactoryConfigurer configurer) { 
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); 
    // This provides all boot's default to this factory, including the message converter 
    configurer.configure(factory, connectionFactory); 
    // You could still override some of Boot's default if necessary. 
    return factory; 

}

Der Ursprung Code kann bei Github

Hilfe wäre sehr dankbar gefunden werden!

+0

IMHO ist es absichtlich. Wenn Ihr Code auf WebLogic ausgeführt wird (die auch Oracle-Produkt) dann das Setup ganz einfach (siehe http://danielveselka.blogspot.cz/2009/07/mdb-3-0-on-weblogic-103-using-oracle -aq.html). Ich fürchte, andere Anwendungsserver bieten JMS keinen AQ-Connectors (Ressourcenadaptern) an. – ibre5041

+0

Nein, ich möchte keine Verbindung zu WebLogic, sondern direkt zu Oracle Advanced Queuing (AQ) herstellen. In meinem Programm ohne Spring Boot verwende ich: '\t \t \t qconFactory = AQjmsFactory.getQueueConnectionFactory (Hostname, ServiceName, Port," Thin "); \t \t \t qcon = qconFactory.createQueueConnection (Benutzername, Passwort); \t \t \t qsession = qcon.createQueueSession (false, Session.AUTO_ACKNOWLEDGE); \t \t \t queueTable = ((AQjmsSession) qsession) .getQueueTable (Benutzername, queueTableName); \t \t \t Warteschlange = ((AQjmsSession) qsession) .getQueue (Benutzername, QueueName); ' – dhmc

+0

Was war zu versuchen zu erklären war. Oracle AQ kann als standardmäßiger Java EE JMS-Provider fungieren. Das könnte Ihren Übergang von ActiveMQ viel einfacher machen. (Tatsächlich würde es keine Änderungen im Quellcode geben). Aber das könnte nur unter bestimmten Umständen funktionieren. – ibre5041

Antwort

1

Die Konfigurations unten Ihre Fragen lösen.

1 - Erstellen Sie die Konfiguration. Für diese Antwort habe ich alle Konfigurationsdateien in der Anwendungsdatei komprimiert. Sie können sie in getrennten Klassen ablegen und so die Sorgen trennen.

@SpringBootApplication 
@EnableJms 
public class Application { 
    private static Random rand = new Random(); 

    @Bean 
    DataSource dataSource() throws SQLException { 
     OracleDataSource dataSource = new OracleDataSource(); 
     dataSource.setUser("yourusername"); 
     dataSource.setPassword("yourpassword"); 
     dataSource.setURL("jdbc:oracle:thin:@yourserver:1521:xe"); 
     dataSource.setImplicitCachingEnabled(true); 
     dataSource.setFastConnectionFailoverEnabled(true); 
     return dataSource; 
    }  

    @Bean 
    public QueueConnectionFactory connectionFactory() throws Exception { 
     return AQjmsFactory.getQueueConnectionFactory(dataSource()); 
    } 

    @Bean 
    public JmsTemplate jmsTemplate() throws Exception { 
     JmsTemplate jmsTemplate = new JmsTemplate(); 
     jmsTemplate.setConnectionFactory(connectionFactory()); 
     jmsTemplate.setMessageConverter(jacksonJmsMessageConverter()); 
     return jmsTemplate; 
    } 

    @Bean 
    public JmsListenerContainerFactory<?> myJMSListenerFactory(QueueConnectionFactory connectionFactory,              DefaultJmsListenerContainerFactoryConfigurer configurer) { 
     DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); 
     // factory.setConcurrency("15-20"); 
     factory.setMessageConverter(jacksonJmsMessageConverter()); 
     configurer.configure(factory, connectionFactory); 
     return factory; 
    } 

    @Bean 
    public MessageConverter jacksonJmsMessageConverter() { 
     MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); 
     converter.setTargetType(MessageType.TEXT); 
     converter.setTypeIdPropertyName("_type"); 
     return converter; 
    } 

    public static void main(String[] args) { 
     ConfigurableApplicationContext context = SpringApplication.run(Application.class, args); 
     JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); 
     for (int i = 0; i < 10; i++) { 
      int waitSecs = rand.nextInt(3); 
      jmsTemplate.convertAndSend("YourQueueName", new Email("[email protected]", "Hello " + i, waitSecs)); 
     } 
    } 
} 

2 - Machen Sie Ihre JMS-Listener

@Component 
public class Receiver { 
    @JmsListener(destination = "YourQueueName", containerFactory = "myJMSListenerFactory") 
    public void receiveEmail(Email email) { 
     System.out.println("Received <" + email + ">"); 
    } 
} 

3 - Maven und Oracle

Sie können die ORACLE6 oder Oracle7 Gläser separat zu Ihrem lib Pfad hinzufügen, wie gezeigt in this post.

Der Rest der Mavan Datei ist ziemlich Standard.

<properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.2.RELEASE</version> 
    </parent> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-activemq</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-databind</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

4 - Business Objects. Objekte wie Email sind Ihre Geschäftsdomäne POJOs. Ich werde sie nicht hier hinstellen.

@Testing, das funktioniert wie Charme ;-)

1

Ich glaube nicht, müssen Sie die myFactory Methode als solche ändern, anstatt Sie connection erstellen müssen, die auf die Oracle-Warteschlange verbindet. Ich hatte eine ähnliche Konfiguration, in Dev habe ich artemis verwendet, um meine JUNIT auszuführen, und in prod habe ich Orakel-Warteschlange verwendet. Unten ist die Klasse, die ich definiert habe, um connectionFactory zu erstellen.

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; 


/** 
* @author Karthik Prasad 
* @since 1.0.0.0 
*  <p> 
*  Configuration file for weblogic JMS connection 
*/ 
@Configuration 
@EnableJms 
@ConfigurationProperties(prefix = "spring.wls.jms") 
@ConditionalOnProperty(prefix = "spring.wls.jms", name = "url") 
public class WLSJmsConfiguration { 

    /** 
    * SJ4J Log instance 
    */ 
    private static final Logger LOG = LoggerFactory.getLogger(WLSJmsConfiguration.class); 

    /** 
    * provider url 
    */ 
    private String url; 
    /** 
    * username of weblogic server using which JNDI connection will be 
    * established 
    */ 
    private String username; 
    /** 
    * password of weblogic server using which JNDI connection will be 
    * established 
    */ 
    private String password; 
    /** 
    * JMS Connection factory name configured in weblogic server 
    */ 
    private String connectionFactoryName; 

    /** 
    * Name of destination queue 
    */ 
    private String targetQueue; 

    /** 
    * The Response Queue 
    */ 
    private String replyQueue; 


    /** 
    * URL to access weblogic Connectionfactory, property is set from properties 
    * file 
    * 
    * @see ConfigurationProperties 
    * @param password 
    *   weblogic url to JNDI 
    */ 
    public void setUrl(final String url) { 
     this.url = url; 
    } 

    /** 
    * username to access weblogic queue, property is set from properties file 
    * 
    * @see ConfigurationProperties 
    * @param username 
    *   weblogic username to access queue 
    */ 
    public void setUsername(final String username) { 
     this.username = username; 
    } 

    /** 
    * Password to access weblogic queue, property is set from properties file 
    * 
    * @see ConfigurationProperties 
    * @param password 
    *   weblogic password to access queue 
    */ 
    public void setPassword(final String password) { 
     this.password = password; 
    } 

    /** 
    * Setter of connection factory name, property is set from properties file 
    * 
    * @see ConfigurationProperties 
    * @param connectionFactoryName 
    *   ConnectionFactory from properties file 
    */ 
    public void setConnectionFactoryName(final String connectionFactoryName) { 
     this.connectionFactoryName = connectionFactoryName; 
    } 

    /** 
    * Setter for {@link #targetQueue} 
    * 
    * @param targetQueue 
    *   the targetQueue to set 
    */ 
    public void setTargetQueue(final String targetQueue) { 
     this.targetQueue = targetQueue; 
    } 

    /** 
    * @param replyQueue 
    *   the replyQueue to set 
    */ 
    public void setReplyQueue(final String replyQueue) { 
     this.replyQueue = replyQueue; 
    } 


    /** 
    * Get JNDI properties from properties file 
    * 
    * @return list of Weblogic jndi properties 
    */ 
    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; 

    } 

    /** 
    * Create JndiTemplate for target weblogic server from provided JNDI 
    * properties 
    * 
    * @return Bean of Jndi Template 
    */ 
    @Bean 
    public JndiTemplate jndiTemplate() { 
     final JndiTemplate jndiTemplate = new JndiTemplate(); 
     jndiTemplate.setEnvironment(getJNDiProperties()); 
     return jndiTemplate; 
    } 

    /** 
    * Creates instance of Jndi Object Factory bean from Jndi Template 
    * 
    * @param jndiTemplate 
    *   Jndi Template for weblogic server 
    * @return Bean of JndiObject Factory 
    */ 
    @Bean(name = "jmsJndiConnectionFactory") 
    public JndiObjectFactoryBean jndiObjectFactoryBean(final JndiTemplate jndiTemplate) { 

     final JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); 
     LOG.debug("Creating Weblogic JMS connection factory"); 
     jndiObjectFactoryBean.setJndiTemplate(jndiTemplate); 
     // connectionFactory name. 
     LOG.debug("ConnectoinFactory Name is {}", connectionFactoryName); 
     jndiObjectFactoryBean.setJndiName(connectionFactoryName); 
     return jndiObjectFactoryBean; 

    } 

    /** 
    * Create Jms Connection factory from Jndi Objectfactory 
    * 
    * @param jndiObjectFactoryBean 
    *   Jndi Object factory bean 
    * @return Returns Jms Connection factory Bean 
    */ 
    @Bean(name = "jmsWlsConnectionFactory") 
    public ConnectionFactory jmsConnectionFactory(final JndiObjectFactoryBean jndiObjectFactoryBean) { 
     final ConnectionFactory connectionFactory = (ConnectionFactory) jndiObjectFactoryBean.getObject(); 
     LOG.debug("ConnectoinFactory is null? {}", connectionFactory == null); 
     return connectionFactory; 
    } 

    /** 
    * Wrap Weblogic Connection Factory around caching factory 
    * 
    * @return 
    */ 
    @Bean(name = "jmsConnectionFactory") 
    @Primary 
    public ConnectionFactory connectionFactoryProxy() { 
     final CachingConnectionFactory jmsConnectionFactory = new CachingConnectionFactory(
       (ConnectionFactory) appContext.getBean("jmsWlsConnectionFactory")); 
     jmsConnectionFactory.setCacheProducers(true); 
     jmsConnectionFactory.setSessionCacheSize(20); 
     return jmsConnectionFactory; 
    } 

    /** 
    * The instance of Target Queue retrieved from JNDI, this bean is created in 
    * dev profile, where one want to run the project in standalone mode but 
    * want to connect to Weblogic Server 
    * 
    * @return Bean of target queue instance 
    */ 
    @Bean 
    public Destination jmsQueueName() { 

     final JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); 
     jndiObjectFactoryBean.setJndiTemplate(jndiTemplate()); 
     jndiObjectFactoryBean.setJndiName(targetQueue); // queue name 
     return (Destination) jndiObjectFactoryBean.getObject(); 
    } 

    /** 
    * Create DestinationResolver to resolve QueueName 
    * 
    * @return Instance of JNDI Destination Resolver 
    */ 
    private DestinationResolver destinationResolver() { 
     final JMSDestinationResolver destinationResolver = new JMSDestinationResolver(); 
     final JndiHelper jndiHelper = new JndiHelper(getJNDiProperties()); 
     destinationResolver.setJndiTemplate(jndiHelper); 
     return destinationResolver; 
    } 

} 

Die application.properties.

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 

Und brauchen Sie wlthint3client zu Ihrem Classpath hinzuzufügen. Ich habe das Glas aus <weblogic_home>\wlserver\server\lib und erstellt Maven Abhängigkeit von Glas und stieß auf meinem lokalen Repo und fügte hinzu, das Glas als Abhängigkeit.

<dependency> 
     <groupId>com.oracle.weblogic</groupId> 
     <artifactId>wlthint3client</artifactId> 
     <version>12.2.1</version> 
     <scope>provided</scope> <!-- comment out this if you are deploying on tomcat or running the application standalone --> 
    </dependency> 
+0

Danke für Ihre Antwort. Großartig für alle, die WebLogic verwenden. Wie kann ich jedoch eine Verbindung zu Oracle Advanced Queuing (AQ) OHNE WEBLOGIC herstellen? Was ist das Spring Boot Oracle AQ entspricht dem ActiveMQ-Code im Getting Started with JMS-Beispiel, siehe [Spring Boot Erste Schritte JMS] [1] Oder das [Spring-Boot-Beispiel-activemq Beispiel auf Github] [ 2] [1]: https://spring.io/guides/gs/messaging-jms/ [2]: https://github.com/spring-projects/spring-boot/tree/master/spring -boot-samples/spring-boot-sample-activemq – dhmc

Verwandte Themen