带有生产者和消费者服务的Apache Camel ActiveMQ

396 阅读3分钟

目录

阅读时间: 4 分钟

阿帕奇-骆驼简介

  • Apache Camel是基于java的集成框架。
  • 它为Spring boot、Quarks、JavaEE、Micro profile、OSGI、standalone等提供运行时支持。
  • Camel是基于企业集成模式的。
  • 拥有300多个组件(连接器)。
  • DSL(java或XML)描述集成流程(路由器)。
  • 几乎可以集成所有的东西。

Apache Camel是一个黑匣子,它从一些终端接收消息并将其发送到其他终端。在这个黑匣子里,消息可以被处理或简单地重定向。

如何在Ubuntu中安装和登录ActiveMQ控制台

1.从Apache上下载ActiveMQ。

$ wget http://archive.apache.org/dist/activemq/5.16.3/apache-activemq-5.16.3-bin.tar.gz

2.2.解压下载的文件。



$ sudo tar -xvzf apache-activemq-5.16.3-bin.tar.gz

3.创建一个名为/opt/activemq 的目录。



$ sudo mkdir /opt/activemq

4.将解压后的文件移到/opt/activemq 目录中。

$ sudo mv apache-activemq-5.16.3/* /opt/activemq

5.创建一个组账户activemq 来运行Apache ActiveMQ。

$ sudo addgroup --quiet --system activemq

6.为该组创建一个用户。

$ sudo adduser --quiet --system --ingroup activemq --no-create-home --disabled-password activemq

7.改变/opt/activemq 目录的权限。

$ sudo chown -R activemq:activemq /opt/activemq

8.创建一个ActiveMQ systemd服务文件来控制Apache ActiveMQ服务。

$ sudo nano /etc/systemd/system/activemq.service

9.9.在该文件中添加以下代码。保存并关闭该文件。

[Unit]
Description=Apache ActiveMQ
After=network.target
[Service]
Type=forking
User=activemq
Group=activemq

ExecStart=/opt/activemq/bin/activemq start
ExecStop=/opt/activemq/bin/activemq stop

[Install]
WantedBy=multi-user.target

10.编辑jetty.xml 配置文件,改变主机。

$ sudo nano /opt/activemq/conf/jetty.xml

11.找到下面这一行。

<property name="host" value="127.0.0.1"/> replace with <property name="host" value="0.0.0.0"/>

12.保存并关闭该文件。

13.重新加载系统守护程序。

$ sudo systemctl daemon-reload

14.启动Apache ActiveMQ服务。

$ sudo systemctl start activemq

15.验证图像中给出的状态。

现在让我们开始做这个项目。在本节中,将创建两个服务。

  • Apache Camel ActiveMQ消息生产者服务
  • Apache Camel ActiveMQ消息消费者服务

Apache Camel消息生产者和消费者服务

项目结构

1.从 spring initializr创建带有Apache Camel依赖项的Spring Boot Maven项目。

2.在pom.xml中导入camel-spring-boot-starter和camel-activemq-starter依赖项。

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

<dependency>
	<groupId>org.apache.camel.springboot</groupId>
	<artifactId>camel-spring-boot-starter</artifactId>
	<version>3.17.0</version>
</dependency>

3.在application.properties中配置Active MQ

server.port = 9091
activemq.broker.url=tcp://localhost:61616

4.编写生产者和消费者路线的代码,如下所示。

创建SpringActiveMQConfig.java

package com.spring.apachecamel.config;

import javax.jms.Queue;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.core.JmsTemplate;

@Configuration
@EnableJms
public class SpringActiveMQConfig {
    @Value("${activemq.broker.url}")
    private String brokerUrl;

    @Bean
    public Queue queue() {
        return new ActiveMQQueue("camelConsumerExample-One");
    }

    @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory() {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setBrokerURL(brokerUrl);
        return activeMQConnectionFactory;
    }

    @Bean
    public JmsTemplate jmsTemplate() {
        return new JmsTemplate(activeMQConnectionFactory());
    }

}

创建Dto Product.java

package com.spring.apachecamel.dto;

import java.io.Serializable;

public class Product implements Serializable {
    private static final long serialVersionUID = 1L;
    private String productId;
    private String name;
    private String productDetails;

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getProductDetails() {
        return productDetails;
    }

    public void setProductDetails(String productDetails) {
        this.productDetails = productDetails;
    }

}

消费者路线

package com.spring.apachecamel.consumer;


import javax.jms.Queue;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.spring.apachecamel.dto.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/consume")
public class Consumer {
    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Queue queue;

    @GetMapping("/message")
    public Product consumeMessage() {

        Product product = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            String jsonMessage = (String) jmsTemplate.receiveAndConvert(queue);
            product = mapper.readValue(jsonMessage, Product.class);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return product;
    }
}

生产者路线

package com.spring.apachecamel.producer;

import com.spring.apachecamel.dto.Product;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.jms.Queue;

@RestController
@RequestMapping("/produce")
public class Producer {
    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Queue queue;

    @PostMapping("/message")
    public Product sendMessage(@RequestBody Product product) {

        try {
            ObjectMapper mapper = new ObjectMapper();
            String productAsJson = mapper.writeValueAsString(product);

            jmsTemplate.convertAndSend(queue, productAsJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return product;
    }
}

5.部署应用程序并使用postman发送消息。

6.让我们 用网址 登录到ActiveMQ控制台。

http://localhost:8161/admin/
credentials as username="admin" and password="admin".

7.7.点击队列 ,确认待处理消息的数量应该是1。

8.让我们使用http://localhost:9091/consume/message端点。

9.验证ActiveMQ控制台。待处理消息的数量应该是零。

总结

我们已经看到了关于Apache Camel的所有内容,以及如何在Ubuntu中安装ActiveMQ。 除此之外,我们还学习了如何 从头开始为SpringBoot ActiveMQ生产者和消费者服务(使用rest端点)编写代码 。

我希望以上的信息能够帮助我们开始学习Apache CamelActiveMQ的集成。请点击这里继续学习关于骆驼的知识。

关于Apache Camel的概述

分享Knol。

相关信息