Automatic subscription not working

Hello all. This is my first post so please be gentle.

I create two connections, conn1 and conn2, and login. I add a RosterListener for each connections roster with print statements in both methods for debug purposes. I then use the conn2’‘s createEntry method to add conn1 to my roster. It doesn’‘t appear that conn1 is auto-subscribing to conn2. Neither of the methods in conn1’‘s Roster’'s RosterListener are being triggered. Please help.

Code:

public class Test {

private String server;

private User u1;

private User u2;

private XMPPConnection conn1;

private XMPPConnection conn2;

private Roster rost1;

private Roster rost2;

public Test() {

init();

doit();

cleanUp();

}

private void init() {

server = “myXMPPHost”;

u1 = new User(“user1”,“password”);

u2 = new User(“user2”,“password”);

try {

conn1 = new XMPPConnection(server);

conn2 = new XMPPConnection(server);

if (conn1.isConnected()) conn1.login(u1.getUserID(), u1.getPassword());

if (conn2.isConnected()) conn2.login(u2.getUserID(), u2.getPassword());

rost1 = conn1.getRoster();

rost2 = conn2.getRoster();

rost1.addRosterListener(

new RosterListener() {

public void presenceChanged(String XMPPAddress) {

System.out.println(u1.getUserID() + " received an update from " + XMPPAddress);

}

public void rosterModified() {

System.out.println(u1.getUserID() + “’'s roster has been modified.”);

}

}

);

rost2.addRosterListener(

new RosterListener() {

public void presenceChanged(String XMPPAddress) {

System.out.println(u2.getUserID() + " received an update from " + XMPPAddress);

}

public void rosterModified() {

System.out.println(u2.getUserID() + “’'s roster has been modified.”);

}

}

);

}

catch(XMPPException xmppe) {

xmppe.printStackTrace();

}

}

public static void main(String args[]) {

Test test = new Test();

System.exit(0);

}

private void doit() {

try {

rost2.createEntry(u1.getUserID(), u1.getUserID(), null);

}

catch(XMPPException xmmpe) {

xmmpe.printStackTrace();

}

delay(4);

}

private void cleanUp() {

rost2.removeEntry(rost2.getEntry(u1.getUserID()));

conn1.close();

conn2.close();

}

public void delay(int seconds) {

try {

Thread.currentThread().sleep(seconds * 1000);

}

catch(InterruptedException ie) {

ie.printStackTrace();

}

}

}

-Dwayne

Dwayne,

The reason why you don’‘t see any notification about user1 is that user1 has never been added to user2’'s roster. You need to pass a valid user XMPP address when adding a user to a roster.

In your example you will need to append “@myXMPPHost” to u1.getUserID() when adding the user to user2’'s roster like this:

rost2.createEntry(u1.getUserID() + "@myXMPPHost", u1.getUserID(), null);

FYI, I’'ll update the comment of the method #createEntry to reflect that the param user must be of the form user@host.

Regards,

– Gato