Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos
RabbitMQ is supported as a service in SCP make it more easier for customer to use it. As we all know, message queue(MQ) is a common communication approach in micro service architecture as it works as a middleware to make the consumer of MQ only need to concentrate on business. Before we use MQ for communication and data transformation, we need to create a TCP link with other services or using HTTP request for communication. We need the exact server address of the services we connected, it will have a lot of limitation. MQ provide a easier approach to manage such connections and the data, it decouples the dependencies of producer and consumer and also provide some advanced feature to manage the transforming data in a better way.RabbitMQ as a famous implementation of message queue, it is widely used and easy to meet high scale and high availably requirement. In this blog we will focus on how to use RabbitMQ provided by SCP step by step and a simple implementation of using RabbitMQ.

 

Using RabbitMQ in Local


We use spring boot project as an example to show you how to use RabbitMQ.

You need to enable rabbitmq_management plugin manually if you want to manage your RabbitMQ in browser. If everything goes fine, you can login to localhost:15762 with username "guest" and password "guest" to see status of local RabbitMQ server.

 

  • Add necessary dependencies


RabbitMQ is already supported in spring boot, you can simple add dependencies as follows:
dependencies {
compile("org.springframework.boot:spring-boot-starter-amqp")
}


  • Add necessary RabbitMQ connection configurations in your application.yml


spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest


  • Register necessary Queue, Exchanges you want to use and bind your queue in the exchange.


@Bean
TopicExchange testExchange() {
return new TopicExchange("test-exchange");
}


@Bean
public Queue testQueue() {
return new Queue("test-queue", true, false, false);
}

@Bean
Binding bindingExchange() {
return BindingBuilder.bind(testQueue()).to(testExchange()).with("test.key");
}

RabbitMQ support different exchange type for different scenarios. We just give a simple example of topic exchange, which is common used.

  • Send your message to RabbitMQ


As spring boot provide "RabbitTemplate"  as a helper to simplify the synchronous RabbitMQ access, we can just send message by invoking method in it. For asynchronous cases, you can use "AsyncRabbitTemplate".
@Autowired
private RabbitTemplate rabbitTemplate;

@Override
public void send() {
this.rabbitTemplate.convertAndSend("test-exchange", "test.key", "Hello World!");
}


  • Receive the message


To receive and handle the message, you can simply use @RabbitListener provided by spring boot.
@RabbitListener(queues = "test-queue")
public void process(String helloWorld){
System.out.println(helloWorld);
}

 

Now you can check if you can send and receive your "Hello World!".

 

 

Using RabbitMQ in SCP


Before you try to use RabbitMQ in SCP, you need to make sure you already have all required account and access to create new service instance and link it to your application.

  • Create a service instance


cf create-service rabbitmq v3.6-dev rabbitmq-service


  • Bind the instance by modify manifest.yml


services:
- rabbitmq-service


  • Add RabbitMQ connection configuration


@Bean
public RabbitConnectionFactory rabbitFactory() {
return connectionFactory().rabbitConnectionFactory();
}

 

After you add connection configurations, you can use RabbitTemplate as normal.

 

We didn't cover all the topics for RabbitMQ like different usage of exchanges, handle exception, blocking queue and so on. You can get more information by visiting https://www.rabbitmq.com/ if you want.