<html>
<head>
<title>ActiveMQ Twitter Connector Service Example</title>
<link rel="stylesheet" type="text/css" href="../../common/common.css" />
<link rel="stylesheet" type="text/css" href="../../common/prettify.css" />
<script type="text/javascript" src="../../common/prettify.js"></script>
</head>
<body onload="prettyPrint()">
<h1>Twitter Connector Service Example</h1>
<p>This example shows you how to configure ActiveMQ to use the Twitter Connector Service.</p>
<p>ActiveMQ 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.
</p>
<p>In this example, incoming connector and outgoing connector 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.</p>
<h2>Example step-by-step</h2>
<p><i>To run the server, simply type <code>mvn-Dtwitter.consumerKey=consumer -Dtwitter.consumerSecret=secret -Dtwitter.accessToken=token -Dtwitter.accessTokenSecret=secret verify</code>
from this directory but replacing the system properties with those of the twitter account you want to use. Then run the example
by using the command <code>mvn -Pexample package</code></p>
<ol>
<li>First we need to create a ClientSessionFactory with Netty transport configuration</li>
<pre class="prettyprint">
<code>csf = ActiveMQClient.createClientSessionFactory(new TransportConfiguration(NettyConnectorFactory.class.getName()));</code>
</pre>
<li>We create a core session with auto-commit mode</li>
<pre class="prettyprint">
<code>session = csf.createSession(true,true);</code>
</pre>
<li>We Create a core producer for queue.outgoingQueue</li>
<pre class="prettyprint">
<code>ClientProducer cp = session.createProducer(OUTGOING_QUEUE);</code>
</pre>
<li>We create a core consumer for queue.incomingQueue</li>
<pre class="prettyprint">
<code>ClientConsumer cc = session.createConsumer(INCOMING_QUEUE);</code>
</pre>
<li>We create a core message that we are going to send</li>
<pre class="prettyprint">
<code>ClientMessage cm = session.createMessage(org.apache.activemq.api.core.Message.TEXT_TYPE,true);
String testMessage = System.currentTimeMillis() + ": twitter connector test example";
cm.getBodyBuffer().writeString(testMessage);</code>
</pre>
<li>We send the message to queue.outgoingQueue</li>
<pre class="prettyprint">
<code>cp.send(cm);</code>
</pre>
<li>We start the session</li>
<pre class="prettyprint">
<code>session.start();</code>
</pre>
<li>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.</li>
<pre class="prettyprint">
<code>ClientMessage received = cc.receive(70 * 1000);
received.acknowledge();
String receivedText = received.getBodyBuffer().readString();</code>
</pre>
<li>And finally, remember to close core session and ClientSessionFactory in a <code>finally</code> block.</li>
<pre class="prettyprint">
<code>finally
{
if(session != null)
{
session.close();
}
if(csf != null)
{
csf.close();
}
}</code>
</pre>
</ol>
</body>
</html>