Monday, July 18, 2016

Working with WSO2 RabbitMQ Inbound

I am writing this blog by considering you already have knowledge about inbound and RabbitMQ. If not please refer my previous blogs. It has brief idea about those.

In WSO2 RabbitMQ inbound, we use inbound as RabbitMQ consumer. It is event based inbound which polls only once to establish a connection with the remote server and then consumes events.  In here AMQP messaging protocol is used. AMQP is a wire-level messaging protocol that describes the format of the data that is sent across the network. If a system or application can read and write AMQP, it can exchange messages with any other system or application that understands AMQP regardless of the implementation language.

Steps to enable RabbitMQ Inbound 
  • Start the WSO2 Server by going <ESB_HOME>/bin by specifying this command sh ./wso2server.sh
  • Create  two simple sequences.  In my case I have created simple sequence names as TestIn and amqpErrorSeq with following configuration
    <?xml version="1.0" encoding="UTF-8"?>
    <sequence name="TestIn" onError="amqpErrorSeq" xmlns="http://ws.apache.org/ns/synapse">
        <log level="full"/>
        <drop/>
    </sequence>



    <?xml version="1.0" encoding="UTF-8"?>
    <sequence name="amqpErrorSeq" xmlns="http://ws.apache.org/ns/synapse">
        <log level="full"/>
        <drop/>
    </sequence>
  •  Go to inbound endpoints in Service Bus menu.
  • Click on Add Inbound endpoint. you will see the page like below.

  • Enter the name of the endpoint and select type as rabbitmq. Click on Next.
  • In the next page specify 

    • Sequence as TestIn.
    • Error Sequence as amqpErrorSeq. 
    • Suspend as false. We need to put as false to make inbound in enable mode. if we put true, the inbound is disabled.
    • Sequential as true.
    • Coordination as true.
    • rabbitmq.connection.factory as AMQPConnectionFactory
    • rabbitmq.server.host.name as localhost
    • rabbitmq.server.port as 5672. Here management console of rabbitmq is 15672. But here we have to mention as 5672.
    • rabbitmq.server.user.name as guest (In my case i am using guest account).
    • rabbitmq.server.password as guest (In my case i am using guest account).
    • rabbitmq.queue.name : specify a queue name.
  • rabbitmq.exchange.name : specify exchange name.
  • If you want to change anything in advanced options, click it and change it.
Thats all for the RabbitMq inbound. That means we have created consumer. For consuming events, we need to provide events. so i have created a sample java producer. That was given below. You can see the output of the message in ESB started terminal.

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Producer {
    private final static String QUEUE_NAME = "queue";
    private final static String contentEncoding = "utf-8";
    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setPort(5672);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        channel.exchangeDeclare("exchange", "direct", true);
        channel.queueBind(QUEUE_NAME, "exchange", "route");
        String message ="<m:placeOrder xmlns:m=\"http://services.samples\">" +
                "<m:order>" +
                "<m:price>100</m:price>" +
                "<m:quantity>20</m:quantity>" +
                "<m:symbol>RMQ</m:symbol>" +
                "</m:order>" +
                "</m:placeOrder>";
        // Populate the AMQP message properties
        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder();
        builder.contentType("application/xml");
        builder.contentEncoding(contentEncoding);

        // Publish the message to exchange
        channel.basicPublish("exchange", QUEUE_NAME, builder.build(), message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
        channel.close();
        connection.close();
    }
}

The out is given below.



Wednesday, July 13, 2016

Working with own SOAP Receiver in WSO2 CEP

Hi all,

In this blog we are going to see from the beginning. Here I am doing one example from the start. You can do it same way for your scenario.

  • First we need to create stream. I created stream named as "soapStream". My created stream is given below.

  • Then we need to create a soap event receiver under Manage->Receivers in Management console. I created event receiver names as "soapTest". My created soap event receiver is given below.

  • Then we need to have publisher to view the output to check whether our input is going inside. I created a logger publisher named as "soapTestLogger" to view the output in terminal.

  • Now we have finished our initial steps. As we mentioned transport as http in Soap receiver which we created, we need to give http soap request. Here I used Postman (which is google chrome apps) to send the request. http address we have to use is http://localhost:9763/services/<ReceiverName>/receive. My receiver name is soapTest. so i used like http://localhost:9763/services/soapTest/receive. In header we have to mention Content-Type as application/xml. In body we have to give our events according to the stream. The body part i gave is given below.

    <soap:Envelope
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
    soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"><soap:Body>
      <events>
        <event>
            <payloadData>
                <ID>70</ID>
                <Name>Test Data</Name>
            </payloadData>
        </event>
    </events>
    </soap:Body>

    </soap:Envelope>
The postman screen shot is given below.


 The output can view in the terminal(CEP started Terminal).

 Now we have finished using soap Receiver with Postman http request. Will see you in next blog. :)

Working with SOAP Receiver Sample in WSO2 CEP

Introduction to WSO2 CEP SOAP Receiver

Events are received by WSO2 CEP server using event receivers, which manage the event retrieval process. WSO2 CEP receives events via multiple transports in JSON, XML, Map, Text, and WSO2Event formats, and converts them into streams of canonical WSO2Events to be processed by the server. SOAP event receiver is used to receive events in XML format via HTTP, HTTPS, and local transports.

Doing the soap sample already available

WSO2 CEP has samples for every Event Receivers it has. You can run the sample very easily like below.
Steps
  • Start the CEP Server sample by going <CEP_HOME>/bin by specifying this command ./wso2cep-samples.sh -sn 0014
    We specify sample number as 0014 because of soap sample receiver is in that. you can view that by going <CEP_HOME>/samples/artifacts/0014/eventreceivers
The receiver is like this.

<?xml version="1.0" encoding="UTF-8"?>
<eventReceiver name="soapReceiver" statistics="disable" trace="disable" xmlns="http://wso2.org/carbon/eventreceiver">
    <from eventAdapterType="soap">
        <property name="transports">all</property>
    </from>
    <mapping customMapping="disable" type="xml"/>
    <to streamName="org.wso2.event.sensor.stream" version="1.0.0"/>
</eventReceiver> 
  • After starting the CEP server, you can see the stream named as "org.wso2.event.sensor.stream" and receiver "soapReceiver" in your CEP management console.
  • As the next step, you need to produce soap events which can be receive by CEP. There is sample java soap producer available in <CEP_HOME>/samples/producers/soap. So you just want to ant build it. For that in terminal, you have to go to above mentioned directory and use below Ant command.

    ant -Durl=http://localhost:9763/services/soapReceiver/receive -Dsn=0014

    Here http://localhost:9763/services/soapReceiver/receive means http location of the receiver. Then we specify the  the sample number. The xml format input data is taken from <CEP_HOME>/samples/artifacts/0014/soapReceiver.txt
  • After this ant command you can see the output in the terminal of CEP started like below.

 Thats All, now you have successfully ran the sample. In my next blog, we will focus on creating own Soap receiver, stream and sending soap request to that. Keep in touch :)




Friday, July 1, 2016

Installing RabbitMQ in Ubunthu

Introduction to RabbitMQ

Initially when I search in Internet i found that RabbitMQ is broker. I also could not understand what they meant by that. After some researches only I could understand. RabbitMQ task is getting the messages and forward it to the receiver. I am sure you are familiar with post office system. In post office post man will get the messages from post box and he will give it to post office. In post office we categories the posts according to the regions and deliver it to relevant places through the postman. This is the procedure of RabbitMQ also. RabbitMQ will act as postman, post office and post box. The thing is post office deals with papers and RabbitMQ deals with blobs of data. A program that sends messages is called as "Producer"(P). Queue is the one which stores messages for a sometime. It is with in RabbitMQ. We can say queue is like post box. A queue is not bound by any limits, it can store as many messages as you like ‒ it's essentially an infinite buffer. Like humans put posts into the post box, a producer can send messages to queue. In this case many consumers can try to receive data from one queue. A "Consumer (C)" is a program that mostly waits to receive messages. Note that the producer, consumer, and broker do not have to reside on the same machine; indeed in most applications they don't. We can write producer and consumer in different languages.

Steps to Install RabbitMQ
  1. You have to check whether RabbitMQ is already in your Ubunthu OS. You can use this code in your terminal.
    sudo rabbitmq-server start

    If yo u get message like "node with name "rabbit" already running on ", then you can know that in your OS, RabbitMQ is already installed. But you can upgrade new version. To upgrade/newly install, you can use following steps.
  2. Execute the following command to add the APT repository.
     sudo add-apt-repository "deb http://www.rabbitmq.com/debian/ testing main"
  3. To avoid warnings about unsigned packages, add our public key to your trusted key list using below command.
    wget https://www.rabbitmq.com/rabbitmq-signing-key-public.asc
  4. After the above command, use this command.
    sudo apt-key add rabbitmq-signing-key-public.asc
  5. Run the following command to update the package list.
    sudo apt-get update
  6.  Install rabbitmq-server package
    sudo apt-get install rabbitmq-server
    In this time, if you had RabbitMQ already, then you could upgrade the version of it. If you don't have it already, new version have installed at this time and start the server automatically.
  7.  You can check whether it start/not by following command.
    sudo rabbitmqctl status
  8. In browser you can type like this http://localhost:15672/In this time if you don't get interface like Figure 1, you have to activate plugins (mentioned in step 9).
  9. Enable plugins.
    sudo rabbitmq-plugins enable rabbitmq_management
  10.  You can use "guest" as username and password.
  11. Yes. I have successfully installed RabbitMQ now. :) In my next blogs, we will focus more on RabbitMQ.

Figure 1