Showing posts with label twitter. Show all posts
Showing posts with label twitter. Show all posts

Saturday, January 10, 2009

Integrating Oracle Coherence with Twitter

Months ago I wrote a program to integrate Calendar with Twitter. This time I integrated Twitter with Coherence data grid. Programmatically this is no brainer - data are being put in a Coherence Cache and then there is a cache listener that publishes the data (message) to twitter. I am a big fan of Twitter. Simple interface, Revolutionary idea and an Awesome channel. So what I did is expanded my implementation of JMS Subscriber for Oracle Coherence and added an interface to tweet the JMS Message. Read more about ezMQ here.... Following is a sample code that is a subscriber of Coherence Topic and a Publisher to Twitter:

public class Subscriber implements MessageListener {

private String un = "<your_twitter_account_id>";
private String pw = "<your_twitter_password>";

public Subscriber() {
}

private void twitter (String message) throws MalformedURLException,
IOException {
String credentials =
new BASE64Encoder ().encode ((un + ":" + pw).getBytes());
URL url = new URL ("http://twitter.com/statuses/update.xml");
URLConnection uC = url.openConnection();
uC.setDoOutput(true);
uC.setRequestProperty("Authorization", "Basic " + credentials);
OutputStreamWriter wR = new OutputStreamWriter (uC.getOutputStream());
wR.write("&status=" + message);
wR.flush();
wR.close();

// -- Get the response back
BufferedReader bR =
new BufferedReader (new InputStreamReader (uC.getInputStream()));
String line = null;
while ((line = bR.readLine()) != null) {
System.out.println(line);
}
bR.close ();
}

public static void main(String[] args)
throws Exception {
Subscriber s = new Subscriber ();
InitialContext ctx = new InitialContext();

// -- Create
TopicConnectionFactory factory =
(TopicConnectionFactory) ctx.lookup("TopicConnectionFactory");

// -- Connecting to Proxy
TopicConnection connection = factory.createTopicConnection();

// -- This is a NamedCache
Topic topic = (Topic) ctx.lookup("Topic");

TopicSession subSession =
connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = subSession.createSubscriber(topic);
subscriber.setMessageListener(s);

System.out.println("Click to end");
System.in.read();
}

public void onMessage(Message message) {
try {
TextMessage tMsg = (TextMessage) message;
String text = tMsg.getText();
// -- Send the message to Twitter
twitter(text);

} catch (JMSException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
Enjoy!

Wednesday, May 30, 2007

Twitterize your SunBird (Calendar)

Aren't you tired of typing all the twitter updates? But still want to let your friends know what you are doing? So here you go... I have twitterized your SunBird. SunBird is Mozilla's open source calendar. My code reads the events from the SunBird database every half hour and updates the Twitter with the first upcoming event. So, if you have a meeting at 11PM and Lunch @ 12:30 then this application will update the twitter with Meeting@11PM. So all you have to do is to update your Calendar (SunBird) with the events and Twitter will be your flash screen. Save some typing Guys!

So what do you do?
I have uploaded the entire application at the download section of ezduck.com so go ahead and download it. Cut and paste the link if the href does not work: http://www.angelfire.com/co3/ashish7s/download/s2t.jar. I have provided the source under the open GPL license, so feel free to enhance it, make it fancier and redistribute it. Just think about me before you do so

How does it work?
SunBird uses the OpenSource SQLite format for its' data storage. So, if you are using any Calendar system which uses the same format, you can use this application against it. Sorry Outlook folks! Microsoft has to open a bit. The application uses the SQLite jdbc driver to connect to the SDB file and selects the future events from the CAL_EVENTS table. It brings the title of the first upcoming calendar event and then connects to the twitter.com to log you in and publishes the message.

How to run the application?
All you need is Java. The manifest file has already been updated to set the mainClass. Open a terminal/console/command prompt and run the following command:
java -Ddb=<location_of_sdb> -Dun=<twitter_login> -Dpw=<twitter_password> -jar s2t.jar
Where,


  • db is the fully qualified location of the SDB file. Usually, Mozilla SunBird saves the calendar in $HOME/.mozilla/sunbird/<id>.default/storage.sdb file on Unix and something similar to "/c:/Documents and Settings/<username>/Application Data/Mozilla/Sunbird/Profiles/<id.default>/storage.sdb" on Windows. Make sure the location of this file is correct. Guys this is your database!

  • un stands for your twitter.com user name. Usually its' your email address.

  • pw stands for your twitter.com password. This is legible text. As SunBird runs locally on your system and so is the terminal which you are running this application on, so its not too bad to type this in. If you are geek enough then go ahead and update the code to encode it.


Future Enhancements:
1. Behind a proxy server? its simple to modify but not done in this release. [Done]
2. A properties file to save the input parameters. [Why to complicate]
3. Targetted messaging from Calendar.
4. From Twitter to your Calendar.

Keep reading the comments on this blog for more options and happy Twittering and Calendaring friends!