Echando código: ¿Monitoreando servicios RPC usando Java, ONC RPC y el protocolo Jabber? (II)

java rpc
El taxista se encargó de decirle a su novia, y esta regó el cuento por allí…

En una primera parte les comentaba como hablar con NFS y a Rstatd desde Java usando RPC. Ahora vamos a terminar la aplicación enviando mensajes usando Jabber.

El protocolo de Jabber es muy completo. Sin embargo con esas características viene la complejidad, así que decidí tomar un atajo y en vez de complicarme la vida viendo como trabaja el protocolo me decidí a usar las libreria Smack (JiveSoftware, OpenSource). Como este articulo no se trata de como instalar un servidor de Jabber entonces vamos a utilizar una cuenta de Google Talk la cual habla el protocolo abierto XMPP .

No me gusta guardar ni usuarios ni passwords en el código, y mucho menos si el código va a estar en Internet :), así que modificamos un poco el archivo build.xml para leer un nuevo archivo de propiedades el cual tendrá los parametros requeridos para esta prueba.

Bueno, y el código super sencillo para probar la parte de la mensajería:


–>

 

1:package com.kodegeek.blog.monitoring.rpc;   
2:   
3:import org.jivesoftware.smack.Chat;   
4:import org.jivesoftware.smack.XMPPConnection;   
5:   
6:import junit.framework.Assert;   
7:import junit.framework.TestCase;   
8:   
9:  
10:public class TestJabber extends TestCase {  
11:     
12:     
13:        protected void setUp() throws Exception {  
14:                super.setUp();  
15:        }  
16:  
17:        /**  
18:         * Test the basic functionality of the Smack API  
19:         */  
20:        public void testJabber() {  
21:                XMPPConnection conn = null;  
22:                Chat chat = null;  
23:             
24:                try {  
25:                        Assert.assertNotNull("Please setup the address for the Google talk server",System.getProperty("jabber.talk.server"));  
26:                        Assert.assertNotNull("Please setup the receiving user for the Google talk server",System.getProperty("jabber.talk.port"));  
27:                        Assert.assertNotNull("Please setup the send address for the Google talk server",System.getProperty("jabber.talk.send.user")); 
28:                        Assert.assertNotNull("Please setup the send password for the Google talk server",System.getProperty("jabber.talk.send.password"));  
29:                        Assert.assertNotNull("Please setup the receiving user for the Google talk server",System.getProperty("jabber.talk.rec.user"));  
30:                     
31:                        conn =  
32:                                new XMPPConnection(  
33:                                System.getProperty("jabber.talk.server"),  
34:                                Integer.parseInt(System.getProperty("jabber.talk.port")));  
35:                     
36:                        Assert.assertNotNull("XMPPConnection is null", conn);  
37:                        Assert.assertTrue("We could not connect to the server", conn.isConnected());  
38:                     
39:                        conn.login(  
40:                               System.getProperty("jabber.talk.send.user"),  
41:                               System.getProperty("jabber.talk.send.password"));  
42:                        Assert.assertTrue("Could not authenticate",conn.isAuthenticated());  
43:                     
44:                        chat = conn.createChat(System.getProperty("jabber.talk.rec.user"));  
45:                        Assert.assertNotNull("Jabber Chat is null", chat);  
46:                     
47:                        /**  
48:                         * This is VERY important. If you don wait a little bit then the message will not  
49:                         * be sent.  
50:                         */  
51:                        Thread.sleep(500);  
52:                        chat.sendMessage("Hey, this is a message from http://KodeGeek.com");  
53:  
54:                } catch (Throwable throwbl) {  
55:                        Assert.fail("Got an exception: " + throwbl.toString());  
56:                        throwbl.printStackTrace();  
57:                } finally {  
58:                        if (conn != null) {  
59:                                conn.close();  
60:                        }  
61:                }  
62:        }  
63:}

Aqui pueden ver como trabaja, una foto vale más que mil palabras:

jabbertest
Mandando mensajes a Jabber desde JUnit. En este caso una cuenta está en Jabber.org y la otra en talk.google.com

Si revizan el código fuente verán que tuve que poner un pequeño hack. Sin embargo no es un gran problema y la librería hace lo que promete.

Como siempre el código fuente y los binarios están disponibles para que jueguen con ellos. Espero esto les de algunas ideas de como monitorear sus servidores. En una última entrega les mostraré como poner todo junto.

Buscar en Technorati: , , , , ,
Buscar en Blogalaxia: , , , , ,