Skip to content

Publishing JSON Events via TCP

Purpose:

This application demonstrates how to configure WSO2 Streaming Integrator Tooling to send sweet production events via TCP transport in JSON format, and view the output on the console.

Prerequisites:

Save this sample.

Executing the Sample:

  1. Open a terminal and navigate to the {WSO2SIHome}/samples/sample-clients/tcp-server directory and run the following command:
    ant -Dtype=json -Dcontext=LowProductionAlertStream
    
  2. Start the Siddhi application by clicking on 'Run'.
  3. If the Siddhi application starts successfully, the following messages appear on the console.
    * PublishTcpInJsonFormat.siddhi - Started Successfully!
    * 'tcp' sink at 'LowProducitonAlertStream' stream successfully connected to 'localhost:9892'.
    
  4. Open the event simulator by clicking on the second icon or press Ctrl+Shift+I.
  5. In the Single Simulation tab of the panel, select values as follows:
    • Siddhi App Name: PublishTcpInJsonFormat
    • Stream Name: SweetProductionStream
  6. In the 'name' field and 'amount' field, enter 'toffee' and '45.24' respectively and click Send to send the event.
  7. Send some more events.
  8. See the output in the terminal of {WSO2SIHome}/samples/sample-clients/tcp-server. You can see output similar to the following:
    [java] [org.wso2.si.tcp.server.TCPServer] : Event{timestamp=1512446413468, data=[toffee, 45.25], isExpired=false}
    [java] [org.wso2.si.tcp.server.TCPServer] : Event{timestamp=1512446425113, data=[coffee, 9.78], isExpired=false}
    [java] [org.wso2.si.tcp.server.TCPServer] : Event{timestamp=1512446442300, data=[chocolate, 78.23], isExpired=false}
    

Notes:

If you need to edit this application while it is running, stop the application -> Save -> Start. If you see the message 'LowProducitonAlertStream' stream could not connect to 'localhost:9892', it could be due to port 9892, defined in the Siddhi application. This port is already being used by a different program. To resolve this issue, please do the following: * Stop this Siddhi application (click 'Run' on menu bar -> 'Stop'). * Change the port 9892 to an unused port in this Siddhi application's source configuration and also change the port number in the tcp-server file. * Start the application and check whether the expected output appears on the console.

@App:name("PublishTcpInJsonFormat")
@App:description('Send events via TCP transport using json format')

define stream SweetProductionStream (name string, amount double);

@sink(type='tcp', url='tcp://localhost:9892/LowProductionAlertStream',
    @map(type='json'))
define stream LowProductionAlertStream (name string, amount double);

from SweetProductionStream
select *
insert into LowProductionAlertStream;