29/03/2014

[TIBCO BusinessEvents] Listen for ActiveSpaces events

When working with TIBCO BusinessEvents, it's possible to create a channel on which to listen for TIBCO ActiveSpaces events such as:


  • TIBAS_EVENT_PUT when a tuple is inserted, overwritten, or updated
  • TIBAS_EVENT_TAKE when a tuple is taken or removed
  • TIBAS_EVENT_EXPIRE when a tuple reaches the end of its time to live and expires from the space
  • TIBAS_EVENT_SEED when there is redistribution after a seeder joins or leaves, and the local node is seeding or unseeding. This is only applicable if the listener distribution scope is SEEDED
  • TIBAS_EVENT_UNSEED when there is redistribution after a seeder joins or leaves, and the local node is seeding or unseeding. This is only applicable if the listener’s distribution scope is SEEDED



In this example I'll demonstrate how to set up this configuration and successfully run a sample demo integrating BusinessEvents (BE), ActiveSpaces (AS) and BusinessWorks (BW).

First, we'll need this software to run the demo:

  • TIBCO Rendezvous*
  • TIBCO Runtime Agent*
  • TIBCO ActiveSpaces (tested on both 2.0.2 and 2.1.2 versions)
  • TIBCO BusinessWorks*
  • TIBCO ActiveSpaces plugin for BW*
  • TIBCO BusinessEvents (including DataGrid component)
NOTE: items marked with * are needed for the demo but this setup may also run on BE+AS alone. It is just to show how it's possible to integrate all these products together. Keep reading until the end for the BE+AS demo only.

Install everything under the same TIBCO_HOME.

Environment setup:



  • Add to your PATH (and LD_LIBRARY_PATH on Unix) variable(s), if not already present, the path to your JRE/JDK installation /bin and /lib folders
  • Add to your PATH (and LD_LIBRARY_PATH on Unix) variable(s), if not already present, the path to your AS installation /bin and /lib folders

Start AS:

1. From a command console, run AS-agent as:

cd AS_INSTALL_DIR\bin

as-agent.exe -metaspace "METASPACE_NAME" -listen "tcp://127.0.0.1:PORT" -name "AGENT_NAME" -discovery "tcp://127.0.0.1:PORT"

eg:

as-agent.exe -metaspace "UserContext" -listen "tcp://127.0.0.1:40001" -name "Agent1" -discovery "tcp://127.0.0.1:40001"

2. From another command console, run AS-admin as:

cd AS_INSTALL_DIR\lib

java -jar as-admin.jar

connect name "METASPACE_NAME" discovery "tcp://127.0.0.1:PORT" listen "tcp://127.0.0.1:ANOTHER_PORT"

eg:

connect name "UserContext" discovery "tcp://127.0.0.1:40001" listen "tcp://127.0.0.1:40002"

Space creation:

Option a. Create a space from AS-admin as:

create space name 'SPACE_NAME' (field name 'FIELD_NAME' type 'FIELD_TYPE' (optional: nullable true/false) [any other fields]) key (fields ('FIELD_NAME'))

eg:

create space name 'Notification' (field name 'notificationid' type 'string' nullable false field name 'customerid' type 'string' field name 'message' type 'string' field name 'status' type 'integer') key (fields ('notificationid'))

You can automate this part, by storing the connect, create space and quit commands in a text file and run the AS-admin as:

java –jar as-admin.jar –i FILE_NAME

Option b. Alternatively, just do a PUT from BW and it will automatically create the space if it's already present

BE setup:

In a new BE project:

  • create an AS connection resource with the same METASPACE_NAME and DISCOVERY_URL parameters as used before, and set a NEW LISTEN port
  • New->Other->TIBCO Channel Wizards->ActiveSpaces Destination and Event (if it works,after clicking on Next you'll see a "member joined: XXX" output in the AS-agent console) and choose on which space to work from the list (if you get a warning or an empty list, it means that the space was NOT created under the same metaspace defined in the AS connection resource)
  • Set these parameters: 
- Distribution role->LEECHER (just for the example, but it's not mandatory, SEEDER would be fine as well)
- Consumption mode->EventListener (we want AS to notify us with an event instead of actively polling the space looking for changes)
- Check for which events you'd like to receive a notification (eg: PUT Event)

When you click on finish, BE will automatically add a Channel and an Event object for you. Just verify that the generated Event object has all the attributes corresponding to the space fields, plus these: consumpion_mode, browser_type and event_type ONCE. You don't have to add them in AS's space too, they're internal for BE.


  • Create a rule that grabs the Event we just generated and does something with it, eg a DebugOut of one of its attributes
  • Prepare the CDD adding the generated destination under Collections/Input Destinations and then add the collection under Agent Classes/Input Destination Collections (or just add the destination directly here)
  • Compile the EAR file and prepare the Run configuration with the references to the project's CDD file and the folder where you created the EAR
  • Run the project. If everything is ok, you'll see in the AS-agent output that a new member has joined the metaspace and in the BE console output that the connection to the metaspace and space was successful:


2014 Mar 28 21:56:53:792 GMT +1 10.0.2.15 Info [main] - [driver.as] connect to metaspace:UserContext
[2014-03-28 21:56:53.917][2396][1220][WARNING][transport] [TcpListenSocket.cpp, 125: Core::TcpListenSocket::listen: ]loopback ip address choosen, this agent may not connect to remote agents
[2014-03-28 21:56:53.917][2396][1220][INFO][transport] ip_address=127.0.0.1 port=40008
[2014-03-28 21:56:58.917][2396][2684][INFO][UserContext.$gmp] manager info received, manager=Agent1 (7f000001-9c41-5335dd3d-216, 127.0.0.1:40001)
[2014-03-28 21:57:03.918][2396][2684][INFO][UserContext.$gmp] -------------------------------
[2014-03-28 21:57:03.918][2396][2684][INFO][UserContext.$gmp] configuring new manager
[2014-03-28 21:57:03.918][2396][2684][INFO][UserContext.$gmp] new manager = Agent1 (7f000001-9c41-5335dd3d-216, 127.0.0.1:40001)
[2014-03-28 21:57:03.918][2396][2684][INFO][UserContext.$gmp] self node = 7f000001-9c48 (7f000001-9c48-5335e215-395, 127.0.0.1:40008)
[2014-03-28 21:57:03.918][2396][2684][INFO][UserContext.$gmp] -------------------------------
[2014-03-28 21:57:04.075][2396][3116][INFO][UserContext.$members] member joined: Agent1 (7f000001-9c41-5335dd3d-216, 127.0.0.1:40001)
[2014-03-28 21:57:04.075][2396][3116][INFO][UserContext.$members] member joined: 7f000001-9c42 (7f000001-9c42-5335dda6-3bc, 127.0.0.1:40002)
[2014-03-28 21:57:04.075][2396][3116][INFO][UserContext.$members] member joined: 7f000001-9c48 (7f000001-9c48-5335e215-395, 127.0.0.1:40008)
2014 Mar 28 21:57:04:091 GMT +1 10.0.2.15 Info [main] - [driver.as] Start to connect space Notification at 2176854396367
[2014-03-28 21:57:04.075][2396][1220][INFO][UserContext.metaspace] Connected metaspace name=[UserContext], listen=[tcp://127.0.0.1:40008], discovery=[tcp://127.0.0.1:40001], member name=[7f000001-9c48] version=2.0.2.042
2014 Mar 28 21:57:04:107 GMT +1 10.0.2.15 Info [main] - [driver.as] Space Notificationconnected at 2176875113531
2014 Mar 28 21:57:04:138 GMT +1 10.0.2.15 Info [main] - [driver.as] This destination is an input destination, SpaceName: Notification
2014 Mar 28 21:57:04:138 GMT +1 10.0.2.15 Info [main] - [driver.as] **************ASChannel:start
2014 Mar 28 21:57:04:138 GMT +1 10.0.2.15 Info [main] - [driver.as] Start space Notification at 2176902134604
2014 Mar 28 21:57:04:138 GMT +1 10.0.2.15 Info [main] - [driver.as] Space Notification started at 2176902260084
2014 Mar 28 21:57:04:154 GMT +1 10.0.2.15 Warning [main] - [driver.as] Resume is not yet implemented for ASDestination
2014 Mar 28 21:57:04:154 GMT +1 10.0.2.15 Info [main] - [studio.core] Hot Deployment disabled
2014 Mar 28 21:57:04:169 GMT +1 10.0.2.15 Info [main] - [runtime.service] Registering all BE-Engine level Group MBeans...
2014 Mar 28 21:57:04:169 GMT +1 10.0.2.15 Info [main] - [runtime.service] All BE-Engine level Group MBeans SUCCESSFULLY registered
2014 Mar 28 21:57:04:169 GMT +1 10.0.2.15 None [main] - [runtime.session] BE Engine 10.0.2.15 started
2014 Mar 28 21:57:04:169 GMT +1 10.0.2.15 Info [main] - [container.standalone] Starting debugger service...

Populate the space:

Now that everything is set up, we'll put something in the space to see that BE is notified of the PUT Event and fires our rule.

Option a. PUT from BW:

In a new BW project:

  • create a Metaspace resource (from ActiveSpaces palette) with the same parameters as before for METASPACE_NAME and DISCOVERY_URL and choose a NEW LISTEN port
  • create a Space under this resource with the same field definition and configuration as the ones created from the AS-admin (or a new one if you chose option B when creating the space).
  • create a Space connection under this space as a LEECHER. Again, not mandatory (you may have chosen SEEDER as well), it's just for this example
  • create a process with a PUT activity on our Space connection
  • run the tester and load our process

If everything goes right, BW puts the tuple in AS and BE receives a notification event which triggers our rule which outputs what we wrote in the DebugOut.

Option b. PUT from BE:

If you want a minimal demo, you can just skip the whole BW part and do a put in AS directly from BE.

In the same BE project:

  • create a timer event that fires every X seconds, depending on how you want to test it
  • create a rule that fires on that event and have it do a createEvent and a sendEvent of our PUT event (yes, it will go in a outbound connection, AS will pick it up and perform the PUT for us)
 rule Rules.OnTimer {  
      attribute {  
           priority = 5;  
           forwardChain = true;  
      }  
      declare {  
           Events.Timer evt;  
      }  
      when {  
             
      }  
      then {  
           Events.YOUR_PUT_EVENT myEvt = Event.createEvent("xslt://");//fill the XSLT to populate the event attributes!            
           Event.sendEvent(myEvt);            
           Event.consumeEvent(evt);  
      }  
 }  


You can find here the sample BE project, BW project and AS scripts.


Note:

Using localhost as discovery and listen IP, an external client cannot join the network since the BE engine does not (correctly) automatically forward the requests received on IP:DISCOVERY_PORT to LOCALHOST:DISCOVERY_PORT

No comments:

Post a Comment

With great power comes great responsibility.

Da grandi poteri derivano grandi responsabilità.