java - 如何将 Spring Boot JMS 从 ActiveMQ 迁移到 Oracle Advanced Queuing

标签 java oracle spring-boot spring-jms advanced-queuing

我正在研究 Spring Boot 和 JMS 示例,是的,我对此很陌生

由于我们使用 Oracle,我想将 Spring Boot 和 JMS 示例从 ActiveMQ 迁移到 Oracle Advanced Queueing。但是,我真的找不到这方面的信息。

据我所知,我需要为 Oracle 版本替换下面的代码,但我没有找到如何替换的方法。

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

原始码可以在Github找到

帮助将不胜感激!

最佳答案

下面的配置将解决您的问题。

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("info@example.com", "Hello " + i, waitSecs));
        }
    }
}

2 - 让您的 JMS 监听器

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

3 - Maven 和 Oracle

您可以将 oracle6 或 oracle7 jar 单独添加到您的 lib 路径,如 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,这很有魅力 ;-)

关于java - 如何将 Spring Boot JMS 从 ActiveMQ 迁移到 Oracle Advanced Queuing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43458500/

相关文章:

java - 在测验中的最后一个问题之后转到 Android Studio 中的另一个 Activity

oracle - 从多个列表填充Grails GSP表

java - 如何在本地网络上连接Java与oracle 8i数据库

java - 使用 spring CrudRepository 自定义查询

mysql - 在 JPA 中创建表时出现问题

java - Java中的内存防护有什么用?

java.util.zip.ZipException : invalid entry CRC (expected 0x0 but got 0xc86c27fe)

java - C# 调用 Java 代码加载非托管 dll

java - 为什么EJB服务器和客户端在同一事务中看到对方的数据库更改?

Spring 批处理 : How to setup a FlatFileItemReader to read a json file?