2017-04-17 7 views
7

의 ActiveMQ에서 오라클 고급 큐에 봄 부팅 JMS를 마이그레이션하는 방법 나는 이전 싶습니다 Spring Boot & ActiveMQ에서 Oracle Advanced Queuing으로의 JMS 예제. 그러나 나는 그것에 대한 정보가 거의 없다.나는 봄 부팅 및 JMS 예를 공부하고 그래, 나는 우리가 오라클로 작업하기 때문에이</p> <p>에 오히려 새로운 해요

오라클 버전에 대한 아래의 코드를 대체해야하지만 아직 그 방법을 찾지 못했습니다. 원점 코드가 Github

도움말에서 찾을 수 있습니다

@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; 

}이 크게 감사하겠습니다!

+0

IMHO는 의도적입니다. 코드가 WebLogic (Oracle 제품이기도 함)에서 실행되는 경우 설치가 매우 쉽습니다 (http://danielveselka.blogspot.cz/2009/07/mdb-3-0-on-weblogic-103-using-oracle 참조). -aq.html). 다른 응용 프로그램 서버가 JMS를 AQ 커넥터 (자원 어댑터)에 제공하지 않는다고 생각합니다. – ibre5041

+0

아니요. WebLogic에 연결하는 대신 Oracle Advanced Queuing (AQ)에 직접 연결하고 싶습니다. 스프링 부트가없는 프로그램에서 다음을 사용합니다 : '\t \t \t qconFactory = AQjmsFactory.getQueueConnectionFactory (hostName, serviceName, port, "thin"); \t \t \t qcon = qconFactory.createQueueConnection (userName, password); \t \t \t qsession = qcon.createQueueSession (false, Session.AUTO_ACKNOWLEDGE); \t \t \t queueTable = ((AQjmsSession) qsession) .getQueueTable (userName, queueTableName); \t \t \t queue = ((AQjmsSession) qsession) .getQueue (userName, queueName); ' – dhmc

+0

설명하려고했던 것은 무엇 이었습니까. Oracle AQ는 표준 Java EE JMS 공급자로 작동 할 수 있습니다. ActiveMQ에서 훨씬 쉽게 전환 할 수 있습니다. (실제로 소스 코드에는 변경 사항이 없습니다.) 그러나 이는 일부 상황에서만 작동 할 수 있습니다. – ibre5041

답변

1

아래 설정은 질문을 해결합니다.

1 - 구성을 만듭니다. 이 대답을 위해 모든 구성 파일을 응용 프로그램 파일에 압축했습니다. 당신은 그것들을 분리 된 수업들에 넣을 수 있습니다.

@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 - 같이 메이븐 및 Oracle

당신은, 당신의 lib 디렉토리 경로에 별도로 oracle6 또는 오라클 7 단지를 추가 할 수 있습니다 -

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

3이 JMS 리스너 확인 this post.

나머지 Mavan 파일은 꽤 표준입니다.

<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 - 비즈니스 객체. 이메일과 같은 객체는 비즈니스 도메인 POJO입니다. 나는 그들을 여기에 두지 않을 것이다.

@Testing이 작품은 매력처럼 ;-)

1

myFactory 메소드를 변경하지 않아도되는 대신 오라클 큐에 연결되는 connectionFactory를 만들어야한다고 생각합니다. 비슷한 구성을 가지고 있었고, dev에 JUNIT을 실행하기 위해 Artemis를 사용했고 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; 


/** 
* @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; 
    } 

} 

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 

는 그리고 당신은 당신의 클래스 패스에 wlthint3client를 추가해야합니다. 나는 <weblogic_home>\wlserver\server\lib에서 항아리를 가지고 jar에서 maven dependency를 생성하고 내 로컬 repo에 push하고 jar를 dependency로 추가했습니다.

<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

답변 해 주셔서 감사합니다. WebLogic을 사용하는 모든 사용자에게 좋습니다. 그러나 WEBLOGIC이 필요없는 Oracle Advanced Queuing (AQ)에 어떻게 연결할 수 있습니까? 그렇다면 Spring 시작하기 JMS 예제에서 ActiveMQ 코드와 동일한 Spring Boot Oracle AQ는 무엇인가요? [Spring Boot Getting Started JMS] [1] 또는 Github의 [spring-boot-sample-activemq 예제] [ 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