Conflict publishing from message listener

Please see the bold lines in the following code:

The first call (in initBot()) to publish works just fine like I expect.

But when I try it again from inside the message listener, I get a presence unavailable message back when I try to publish and the stream closes.

public class AlertBot {

private String userName = null;

private String password = null;

private String domainName = null;

private int specificPort = -1;

private XMPPConnection conn = null;

private boolean verbose = true;

private String messengerBotName = “messengerbot@domain”;

private String blogNode = “”;

public AlertBot(String domainName, String username,

String pword, String messengerName) throws XMPPException {

setDomainName(domainName);

setUserName(username);

setPassword(pword);

setMessengerBotName(messengerName);

setBlogNodeName(username);

initBot();

}

public AlertBot(String domainName, int port,

String username, String pword,

String messengerName) throws XMPPException {

setDomainName(domainName);

setSpecificPort(port);

setUserName(username);

setPassword(pword);

setMessengerBotName(messengerName);

initBot();

}

/**

  • Listen for disconnect, if we do, then shut down

*/

private void addConnectListener() {

conn.addConnectionListener(new ConnectionListener() {

public void connectionClosed() {

debugPrint(“Connection Closed”);

// if(conn != null) {

// if(conn.isConnected()) {

// conn.disconnect();

// }

// }

// System.exit(0);

}

public void connectionClosedOnError(Exception arg0) {

//throw new UnsupportedOperationException(“Not supported yet.”);

}

public void reconnectingIn(int arg0) {

//throw new UnsupportedOperationException(“Not supported yet.”);

}

public void reconnectionSuccessful() {

//throw new UnsupportedOperationException(“Not supported yet.”);

}

public void reconnectionFailed(Exception arg0) {

//throw new UnsupportedOperationException(“Not supported yet.”);

}

});

}

private void setBlogNodeName(String username) {

username = username.toLowerCase();

int index = username.indexOf("@");

if(index != -1) {

username = username.substring(0, index);

}

int indexOfBot = username.indexOf(“bot”);

StringBuffer newName = new StringBuffer();

for(int i = 0; i < username.length(); i++) {

String s = username.toCharArray()[i] + “”;

if(i ==0) {

s = s.toUpperCase();

} else if (i == indexOfBot) {

s = s.toUpperCase();

}

newName.append(s);

}

newName.append(“History”);

blogNode = newName.toString();

debugPrint("blog name: " + blogNode);

}

private void createChatListener() {

ChatManager chatmanager = conn.getChatManager();

Chat newChat = chatmanager.createChat(getMessengerBotName(), new MessageListener() {

public void processMessage(Chat chat, Message message) {

debugPrint("Received message: " + message);

debugPrint("Msg Type: " + message.getType());

if(message.getType() == Message.Type.chat) {

debugPrint("Chat body: " + message.getBody());

//forwardMessageToRoster(message);

** publishMessageToBlog(message.getBody());**

}

}

});

}

private void publishMessageToBlog(final String message) {

debugPrint(“In Publish Message.”);

XMPPUtil.PublishEntryToNode(conn, userName, “pubsub.”+

domainName, userName,

message, blogNode);

debugPrint(“After Publish Message.”);

}

private void forwardMessageToRoster(Message message) {

if(conn != null) {

if(conn.isConnected()) {

Roster r = conn.getRoster();

Collection res = r.getEntries();

Iterator iter = res.iterator();

ChatManager chatmanager = conn.getChatManager();

while(iter.hasNext()) {

RosterEntry re = (RosterEntry) iter.next();

debugPrint("Roster Entry: " + re.getUser());

Chat newChat = chatmanager.createChat(re.getUser(), null);

try {

debugPrint("Send message: " + message.getBody());

debugPrint("To user: " + newChat.getParticipant());

newChat.sendMessage(message.getBody());

} catch (XMPPException ex) {

ex.printStackTrace();

//Logger.getLogger(AlertBot.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

}

}

private void debugPrint(String s) {

if(verbose) {

System.out.println(s);

}

}

private void initBot() throws XMPPException {

debugPrint("courier = " + getMessengerBotName());

connect();

makePresenceAvailable();

** publishMessageToBlog(userName + " connected.");**

createChatListener();

}

private void connect() throws XMPPException {

if(verbose) {

XMPPConnection.DEBUG_ENABLED = true;

}

ConnectionConfiguration config = null;

if(getSpecificPort() == -1) {

config = new ConnectionConfiguration(getDomainName(), 5222);

config.setReconnectionAllowed(true);

conn = new XMPPConnection(getDomainName());

} else {

config = new ConnectionConfiguration(getDomainName(), getSpecificPort());

config.setReconnectionAllowed(true);

conn = new XMPPConnection(config);

}

conn.connect();

debugPrint("Connection is connected: " + conn.isConnected());

addConnectListener();

debugPrint("Attempting to login as: " + getUserName() + " " + getPassword());

conn.login(getUserName(), getPassword());

}

private void disconnect() {

if(conn != null) {

if(conn.isConnected()) {

conn.disconnect();

}

}

}

private void makePresenceAvailable() {

Presence presence = new Presence(Presence.Type.available);

debugPrint(“Sending presence packet”);

conn.sendPacket(presence);

}

/**

  • @return the userName

*/

public String getUserName() {

return userName;

}

/**

  • @param userName the userName to set

*/

public void setUserName(String userName) {

this.userName = userName;

}

/**

  • @return the password

*/

public String getPassword() {

return password;

}

/**

  • @param password the password to set

*/

public void setPassword(String password) {

this.password = password;

}

/**

  • @return the domainName

*/

public String getDomainName() {

return domainName;

}

/**

  • @param domainName the domainName to set

*/

public void setDomainName(String domainName) {

this.domainName = domainName;

}

/**

  • @return the specificPort

*/

public int getSpecificPort() {

return specificPort;

}

/**

  • @param specificPort the specificPort to set

*/

public void setSpecificPort(int specificPort) {

this.specificPort = specificPort;

}

/**

  • @return the messengerBotName

*/

public String getMessengerBotName() {

return messengerBotName;

}

/**

  • @param messengerBotName the messengerBotName to set

*/

public void setMessengerBotName(String messengerBotName) {

this.messengerBotName = messengerBotName;

}

public static void main(String[] args) {

AlertBot bot = null;

String domain = null;

String username = null;

String password = null;

String messengerName = null;

System.out.println("args length: " + args.length);

for(int i = 0; i < args.length; i++) {

System.out.println(args[i]);

}

try {

// make a connection and login

if(args.length < 1) {

domain = “domain”;

username = “alertbot”;

password = “password”;

messengerName = “messengerbot@domain”;

} else {

if(args.length ==1 ) {

String[] array = args[0].split(" ");

if(array.length == 4) {

domain = array[0];

username = array[1];

password = array[2];

messengerName = array[3];

}

} else if(args.length ==4) {

domain = args[0];

username = args[1];

password = args[2];

messengerName = args[3];

}

}

// start up our bot

bot = new AlertBot(domain, username, password, messengerName);

} catch (XMPPException ex) {

ex.printStackTrace();

} finally {

}

} // end main

} // end AlertBot class