DBA > Articles

Build a Google Talk Client Using Oracle ADF Faces Rich Client and the Active Data Service

By: Lucas Jellema
To read more DBA articles, visit http://dba.fyicenter.com/article/

Part 1: Creating a Java Application That Speaks Google Talk

How to Set Up Smack and Connect to Google Talk for Programmatic Chats (Reading and Sending Messages)

Start Oracle JDeveloper 11g. Create a new application—call it, for example, GoogleTalk. Also create a new project; for example, GoogleTalkJavaClient.

Choose No Template [All Technologies] from the Application Template menu. Open the Project Properties dialog box from the right mouse button menu on the Project node or from the Tools menu. Go to the Libraries and Classpath node.

Add the smack.jar and the smackx.jar file archives to the project: click the Add JAR/Directory button. Browse to the Smack libraries and file, select both smack.jar and smackx.jar and click the Select button.

The JAR file is added to the project.

Click the OK button.

We will now create a class that will be able to send IM messages via Google Talk. Create a MessageSender class in package otn.adf.googletalk. Tell Oracle JDeveloper to add a main method.

package otn.adf.googletalk;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import  org.jivesoftware.smack.packet.Message;
public class  MessageSender {
private static String username =  "YOUR_GOOGLE_TALK_USERNAME"; 
private static String password =  "YOUR_GOOGLE_TALK_PASSWORD"; 
    ConnectionConfiguration connConfig;
    XMPPConnection connection;
    public MessageSender() throws  XMPPException {
connConfig = new  ConnectionConfiguration
("talk.google.com", 5222,  "gmail.com");
        connection = new XMPPConnection(connConfig);
        connection.login(username, password);
public void sendMessage(String to, String  message ) {
        Message msg = new Message(to,  Message.Type.chat);
    public void disconnect() {
    public static void main(String[] args)  throws XMPPException {
       MessageSender messageSender = new  MessageSender();
             "Hello You. This is my first message sent
              programmatically using Smack API  and Google Talk.");

Make sure you put in your own Google Talk account name and password on lines 10 and 11. Also put in your account—or your friend’s—on line 36, as the destination of this “Hello, world of instant messaging.”

The constructor of the MessageSender class creates a connection to the Google Talk server and logs in (line 17–21). When the MessageSender instance is created, the sendMessage method is invoked, with a destination (who to send the message to) and the message itself. Finally, the connection is closed in the disconnect method.

When you run the application, you will quickly receive your first Google Talk message sent by your own application through the Google Talk server:

One of the nice features of instant messaging is presence—the indication of which of our friends and contacts are available. Google supports presence and so does the Smack API.

We first can tell the world about our own presence, by executing these lines of code:

// tell the world (or at least our friends) that we are around Presence presence = new Presence(Presence.Type.available);

Note: We can choose from several presence modes: available, chat, away, xa (extended away), and dnd (do not disturb).

Even more interestingly for us is that our program can learn the presence of all our contacts—called our “roster” in IM terms—or a specific contact. The list of all our contacts is retrieved like this:

Roster roster = connection.getRoster();
Collection iter = roster.getEntries();
for (RosterEntry entry : iter) {
System.out.println(entry.getName() + " (" + entry.getUser() + ")");

Receiving Messages
If all we could do was send messages, we would be in for a dull conversation. The next step we will make is adding listening capabilities to our “application.” We will create a MessageListener class that will look remarkably much like the MessageSender. In fact, let’s create it by first saving the MessageSender.java file as MessageListener.java. Then use search and replace to replace all occurrences of the word MessageSender with MessageListener.

Then have the class implement the PacketListener interface by adding implements PacketListener to the class specification. The only thing this interface requires us to do is implement the method processPacket, like this for example:

public void processPacket(Packet packet) {
Message message = (Message)packet;
System.out.println("Message (from: "+message.getFrom()+"): "+message.getBody());

After we have registered our class as listener for messages with the connection, this method will be called whenever a message is sent by the Google Talk server to our connection. Registration of our class as a listener for chat messages to our account is done by adding these lines of code at the end of the constructor for MessageListener():

// to listen for incoming messages on this connection
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener((PacketListener)this, filter);

Note: We need to add the following import statements to our class; however, Oracle JDeveloper will by and large do this for us as we add the code described above.

import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Packet;

We are now ready to receive messages. If we change the main method of our MessageReceiver class as follows, we will receive our own message:

public static void main(String[] args) throws XMPPException,
InterruptedException {
MessageListener messageListener = new MessageListener();
"Hello You. This is my second message sent programmatically using Smack API and Google Talk.");
// listen for 3 seconds

Of course, instead of talking to yourself, it would be much better to listen to chat messages sent to you by your friends. If none are available at the present, Google Talk offers a series of “friends” that you can converse with. These so-called bots are programmatic Google Talk accounts that offer language translation. The bots have accounts like en2zh@bot.talk.google.com and nl2en@bot.talk.google.com, composed of the two-letter abbreviations for the source and target languages. (See http://googletalk.blogspot.com/2007/12/merry-christmas-god-jul-and.html for the announcement of the Google Talk translation bots).

We can simply send a chat message to a Google bot and it will send us a return message—great for quickly finding translations for simple words or phrases. We can change the main method yet again, to have it count in French from 1 to 10 (although, of course, you do not need a translation bot for doing so):

MessageListener messageListener = new MessageListener();
String[] englishCounting = new String [] {"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten"};
for (int i=0;i messageListener.sendMessage("en2fr@bot.talk.google.com", englishCounting[i]);
// add the slight pause in order to increase the chances of
// receiving the replies in the right order

The result of running the application this time is a count in French:

Full article...

Other Related Articles

... to read more DBA articles, visit http://dba.fyicenter.com/article/