Saturday, February 16, 2013

Writing Custom Broker for WSO2 CEP

In this post I'll explain how to create a simple Custom Broker in WSO2 CEP 2.1.x.

You can find a sample template Custom Broker project from here.

The first step is to create a Java project (here I have used Apache Maven),
then create an appropriate Broker Type by extending "org.wso2.carbon.broker.core.BrokerType" and an appropriate Broker Type Factory by extending the  "org.wso2.carbon.broker.core.BrokerTypeFactory" from the jar org.wso2.carbon.broker.core-4.0.7.jar

org.wso2.carbon.broker.core-4.0.7.jar will be available in the WSO2 public Maven Repository, which you can add to your project pom.xml as:

<repositories>
    <repository>
        <id>wso2-maven2-repository</id>
        <name>WSO2 Maven2 Repository</name>
        <url>http://dist.wso2.org/maven2</url>
    </repository>
</repositories>

else you can also find that at CEP_HOME/repository/components/plugins/ as org.wso2.carbon.broker.core_4.0.7.jar

When extending the BrokerType, the BrokerTypeImpl has to implement the following methods.

package org.wso2.carbon.broker.core;

import org.apache.axis2.engine.AxisConfiguration;
import org.wso2.carbon.broker.core.exception.BrokerEventProcessingException;

/**
 * This is a broker type. these interface let users to publish subscribe messages according to
 * some type. this type can either be local, jms or ws
 */
public interface BrokerType {

    /**
     * object which describes this type. it contains the name and available properties.
     *
     * @return - type dto
     */
    BrokerTypeDto getBrokerTypeDto();

    /**
     * subscribe to the connection specified in the broker configuration.
     *
     * @param topicName           - topic name to subscribe
     * @param brokerListener      - broker type will invoke this when it receive events
     * @param brokerConfiguration - broker configuration details
     * @throws BrokerEventProcessingException - if can not subscribe to the broker
     */
    String subscribe(String topicName,
                     BrokerListener brokerListener,
                     BrokerConfiguration brokerConfiguration,
                     AxisConfiguration axisConfiguration) throws BrokerEventProcessingException;

    /**
     * publish a message to a given connection with the broker configuration.
     *
     * @param topicName           - topic name to publish messages
     * @param object              - message to send
     * @param brokerConfiguration - broker configuration to be used
     * @throws BrokerEventProcessingException - if the message can not publish
     */
    void publish(String topicName,
                 Object object,
                 BrokerConfiguration brokerConfiguration) throws BrokerEventProcessingException;


    /**
     * publish test message to check the connection with the broker configuration.
     *
     * @param brokerConfiguration - broker configuration to be used
     * @throws BrokerEventProcessingException - if the message can not publish
     */
    void testConnection(BrokerConfiguration brokerConfiguration) throws BrokerEventProcessingException;

    /**
     * this method unsubscribes the subscription from the broker.
     *
     * @param topicName
     * @param brokerConfiguration
     * @throws BrokerEventProcessingException
     */
    void unsubscribe(String topicName,
                     BrokerConfiguration brokerConfiguration,
                     AxisConfiguration axisConfiguration, String subscriptionId)
            throws BrokerEventProcessingException;

}

Here you have to write the appropriate client code for both sending and receiving events.

The publish() method in the BrokerType will be called when CEP is sending an output, your implementaion to publish the message needs to go under the publish() method. The input parameter message Object, of the publish() method can be either of the following,
  • java.lang.String
  • java.util.Map
  • org.apache.axiom.om.OMElement
  • org.wso2.carbon.databridge.commons.Event
In your implementation you don't need to support all the message types, and supporting the appropriate type will be adequate.  This is because at the bucket output mapping, user can select the appropriate message type mapping when sending the events.

At the same time when a subscription was made for the subscribe() method the appropriate BrokerListener will be also provided, which is associated with the topic. BrokerListener also have a method onEvent(Object message) to which your client code receiving  the incoming events has to pass the received messages. Here the events passed to the onEvent() must have one of the following format,
  • java.util.Map
  • org.apache.axiom.om.OMElement
  • org.wso2.carbon.databridge.commons.Event
When extending the BrokerTypeFactory, the BrokerTypeFactoryImpl has to implement the getBrokerType() method.

package org.wso2.carbon.broker.core;

public interface BrokerTypeFactory {
    BrokerType getBrokerType();
}

Then you have to build the jar and deploy that to the CEP server.

If its a plain jar you have to add that to  CEP_HOME/repository/components/lib or
If its a OSGi bundle then you can add that to CEP_HOME/repository/components/dropings

Next, you have to create a file called "broker.xml" at wso2cep-2.0.1/repository/conf directory, and add the implantation class as in the following XML:

<brokerTypes xmlns="http://wso2.org/carbon/broker">
    <brokerType class="org.test.cep.broker.BrokerTypeFactoryImpl"/>
</brokerTypes>

Note: In the above code snippet change the "org.test.cep.broker.BrokerTypeFactoryImpl" with your implementation class name.

Finally, restart & login to the CEP server,  go to Configure -> Broker -> Add, and there you will be able configure your custom broker.



You will be able to download a sample template project from : https://dl.dropbox.com/u/17922825/blog/cep-ext-broker.zip

24 comments:

  1. Custom essays are the traditional essays focusing a topic and it is expected that the students exhibit their talents. This site is a huge help in writing an essay professionally.

    ReplyDelete
  2. approaching this mid year, and again treated us with a this site perceiving investigation of contemporary Japanese society.

    ReplyDelete
  3. Hi, I have checked your query it is working... If you tried that with some other external broker like emailbroker... then it is working... I thing the issue is with the AgentTestServer... I'm looking in to that further and give you feedback on this web site.

    ReplyDelete
  4. Hi, I have some question about cep.
    As I know that cep uses cassandra database, but the business events are stored in cassandra? How do I check which content are stored in Cassandra?

    When I work with BAM, I can open the cassandra database to find the business events.
    But when running CEP, I cannot.
    Can you give me some help?

    ReplyDelete
  5. Hi,
    For CEP no data will be stored, all the data in kept in-memory, unless you enable persistence snapshot in the bucket.

    So you cannot find any in the Cassandra.

    Suho

    ReplyDelete
  6. Thanks for your response, I got it.

    ReplyDelete
  7. Hi, I have some some questions about custom broker.

    I tried custom broker using example from this blog article.
    I downloaded a sample template Custom Broker from "https://dl.dropbox.com/u/17922825/blog/cep-ext-broker.zip",
    I put cep-broker-1.0-SNAPSHOT.jar to CEP_HOME/repository/components/lib.

    Next, I put broker.xml to CEP_HOME/repository/conf.
    Following contents of broker.xml:





    And, restart and login to the management console.

    But, I accessed Configure -> Broker -> Add on management console each time, following error messages got dumped to Monitor -> System Logs

    Error during rendering
    Error during rendering
    IO Error executing tag: JSPException while including path '/CEP/cep_buckets.jsp'. ServletException while including page.
    IO Error executing tag: JSPException while including path '/CEP/cep_buckets.jsp'. ServletException while including page.
    JSPException while including path '/CEP/cep_buckets.jsp'.
    JSPException while including path '/CEP/cep_buckets.jsp'.
    Exception occurred while trying to invoke service method getEngineProvidersInfo
    Exception occurred while trying to invoke service method getEngineProvidersInfo
    Exception occurred while trying to invoke service method getEngineProvidersInfo
    Exception occurred while trying to invoke service method getEngineProvidersInfo

    Also, I accessed Configure -> Broker -> List, Main -> CEP Buckets -> Add and List, same error messages got dumped.

    I entreat you to help me.

    I'm sorry.
    I didn't mean to go on so long.

    ReplyDelete
  8. I can only guess how hard it is sometimes to find and complete a serious task at university. As a former student every man can name several logical reasons for youngsters to look for writing essays online at papersmart during their studies. It becomes necessary for anyone that critical point of view is a must for a prosperous mind. I ask only the best writing companies to perform my scheduled tasks and make a good arrangements of my own ideas.

    ReplyDelete
  9. I don’t love to venture accompanying poets who warranty to smart custom writing entirely to be disappointed at the ultimate moment. I permit scan the various thumbs on the couriers on how to explore a worthy correspondent. How do I explore a estimable sole who inscribes rapid?

    ReplyDelete
  10. Very useful tips. I really appreciate your post.Thank you once again.

    Bahamas Customs Broker

    ReplyDelete
  11. Thanks a lot for showing us how to create a simple custom broker and for even going ahead to provide us with sample template Custom Broker project. It appears to be so simple yet so complicated, I can only hope that I will be able to successfully configure the custom broker once I'm done following the steps given above. Do you want to submit a quality research project with the help of professional writers? If yes, then do not hesitate to follow the link below:
    Online Proofreading Help

    ReplyDelete
  12. Here we have some good sample templates of Custom Broker project.I think it is more useful for people who need templates. Hope this post will help to avoid the complications regarding this kind of templates.
    Best essay writing service

    ReplyDelete
  13. I have read many blogs in the net but have never come across such a well written blog. Good work keep it up
    discount codes

    ReplyDelete
  14. Hi guys,
    Thank you so much for this wonderful article really!
    If someone want to know more about the brokers I think this is the right place for you!

    ReplyDelete
  15. Need a customs brokers ? Check out here for a lot of usefull things!

    ReplyDelete
  16. I hope it will help a lot for all. Thank you so much for this amazing post webstagram

    ReplyDelete
  17. Wow i can say that this is another great article as expected of this blog.Bookmarked this site.. City Index review bitcoin

    ReplyDelete
  18. In the world of crypto, bots have been present since the days of Mt. Gox. They are getting more and more advanced as time goes on. Some exchanges are even advertising faster connection speeds for so called “high frequency” Crypto bot

    ReplyDelete
  19. This is very interesting content! I have thoroughly enjoyed reading your points and have come to the conclusion that you are right about many of them. You are great.

    Spiritual Reading Near Me,
    Psychic Indiana,

    ReplyDelete
  20. Hey very nice blog!! Guy .. Beautiful .. Superb .. I will bookmark your site and take the feeds also…I’m happy to seek out so many useful information right here within the submit,

    we’d like develop more strategies in this regard, thanks for sharing.
    Lord Vishnu Puja In Texas,
    Ganesh Puja In Texas,
    Husband Wife Problems In Texas,

    ReplyDelete
  21. Thanks for sharing superb informations. Your web site is very cool. I’m impressed by the details that you have on this blog. It reveals how nicely you perceive this subject. Bookmarked this

    web page, will come back for more articles. You, my pal, ROCK! I found simply the info I already searched all over the place and just couldn’t come across. What an ideal web-site.

    Best Indian astrologer in Texas,
    Astrologer in Texas,
    Astrologer in Georgia,
    Evil Spirit Removal In Texas,

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete
  23. Hey very nice blog!! Guy .. Beautiful .. Superb .. I will bookmark your site and take the feeds also…I’m happy to seek out so many useful information right here within the submit,

    we’d like develop more strategies in this regard, thanks for sharing.
    Astrologer in Bangalore
    Famous Astrologer in Bangalore

    ReplyDelete
  24. AFM Logistics Pvt Ltd is an international freight forwarding and customs clearing company established in Delhi. The company was constituted in 2012 and is indulged in providing complete logistics solution. The company has its own setup and wide network of agents throughout the world. Best Custom Clearing Company in Delhi . They are the best air cargo and ocean freight forwarding company in Delhi, India. AFM Logistics Pvt Ltd has been working as Import and Export Agent in India since 2012. They have been providing personal baggage shipping services in India for a very long time.

    ReplyDelete