# Twitter Connector Service Example

This example shows you how to configure HornetQ to use the Twitter Connector Service.

HornetQ supports 2 types of Twitter connector, incoming and outgoing. Incoming connector consumes from twitter and forwards to a configurable address. Outgoing connector consumes from a configurable address and forwards to twitter.

In this example, incoming connector and outgoing conenctor is related to same twitter account. So if you send a message to an outgoing address, outgoing connector forwards it to twitter, and then incoming connector consumes it and forwards to incoming address.

All you need to do is edit the server0/hornetq-configuration.xml to use twitter connector.

          <twitter-connectors>
             <!-- consumes from twitter and forwards to queue.incomingQueue -->
             <incoming-twitter-connector name="my-incoming-tweets">
                <queue-name>queue.incomingQueue</queue-name>
                <twitter-account>
                   <username>${twitter.username}</username>
                   <password>${twitter.password}</password>
                </twitter-account>
                <interval-seconds>60</interval-seconds>
             </incoming-twitter-connector>

             <!-- consumes from queue.outgoingQueue and forwards to twitter -->
             <outgoing-twitter-connector name="my-outgoing-tweets">
                <queue-name>queue.outgoingQueue</queue-name>
                <twitter-account>
                   <username>${twitter.username}</username>
                   <password>${twitter.password}</password>
                </twitter-account>
             </outgoing-twitter-connector>
          </twitter-connectors>

## Example step-by-step

To run the example, simply type `./build.sh` (or `build.bat` on windows) from this directory. Also please remember to set the twitter user/pass in environment variable Before you run.

on Linux:

         export TWITTER_USERNAME=your_twitter_username
    export TWITTER_PASSWORD=your_twitter_password
    ./build.sh

on Windows:

         set TWITTER_USERNAME=your_twitter_username
    set TWITTER_PASSWORD=your_twitter_password
    build.bat

1. First we need to create a ClientSessionFactory with Netty transport configuration

               csf = HornetQClient.createClientSessionFactory(new TransportConfiguration(NettyConnectorFactory.class.getName()));

2. We create a core session with auto-commit mode

               session = csf.createSession(true,true);

3. We Create a core producer for queue.outgoingQueue

               ClientProducer cp = session.createProducer(OUTGOING_QUEUE);

4. We create a core consumer for queue.incomingQueue

               ClientConsumer cc = session.createConsumer(INCOMING_QUEUE);

5. We create a core message that we are going to send

               ClientMessage cm = session.createMessage(org.hornetq.api.core.Message.TEXT_TYPE,true);
    String testMessage = System.currentTimeMillis() + ": twitter connector test example";
    cm.getBodyBuffer().writeString(testMessage);

6. We send the message to queue.outgoingQueue

              cp.send(cm);

7. We start the session

               session.start();

8. We will receive a message from queue.incomingQueue. Outgoing connector forwards a message(we sent before) to twitter immediately. Since incoming connector consumes from twitter and forwards to queue.incomingQueue every 60 seconds, It will be received in 60+x seconds.

               ClientMessage received = cc.receive(70 * 1000);
    received.acknowledge();
    String receivedText = received.getBodyBuffer().readString();

9. And finally, remember to close core session and ClientSessionFactory in a `finally` block.

               finally
    {
        if(session != null)
        {
           session.close();
        }
        if(csf != null)
        {
           csf.close();
        }
    }