How to receive / handle sending an XMPP message to a user running Spring Boot / Integration

I currently have a Spring Boot startup application that registers with an XMPP server as a specific user. I can send messages to remote clients, etc. But now I want to receive messages on the server if clients send messages to that specific user who is logged in through Spring Boot.

I tried to hook up a service activator that actually receives the message, but the content of the message is placed in the "To" field and the "from" property is null. So I'm puzzled as to how to connect things.

Below is the xmpp inbound adapter configuration:

<!-- incoming xmpp configuration -->
<int-xmpp:inbound-channel-adapter auto-startup="true" xmpp-connection="xmppConnection" channel="incomingMessagesChannel"/>
<channel id="incomingMessagesChannel">
    <interceptors>
        <wire-tap channel="incomingMessagesLogger"/>
    </interceptors>
</channel>
<logging-channel-adapter id="incomingMessagesLogger" logger-name="incoming logger" log-full-message="true" />
<service-activator ref="messageEventHandler" method="handle" input-channel="incomingMessagesChannel"/>

      

And the class that handles incoming messages:

import org.jivesoftware.smack.packet.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class MessageEventHandler {

    private static final Logger logger = LoggerFactory.getLogger(MessageEventHandler.class);

    public void handle(Message message) {
        System.out.println(message.getFrom());
        System.out.println(message.getTo());
        System.out.println(message.getBody());
    }
}

      

So when I post a message to the server, the output is:

  • System.out.println(message.getFrom());

    null

  • System.out.println(message.getTo());

    is the actual content of the post
  • System.out.println(message.getBody());

    null

What am I missing?

+3


source to share


1 answer


The solution to this problem was to use the correct Message implementation!



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

@Component
public class MessageEventHandler {

    private static final Logger logger = LoggerFactory.getLogger(MessageEventHandler.class);

    public void handle(Message message) {
        MessageHeaders headers = message.getHeaders();

        String from = (String) headers.get("xmpp_from");
        String to = (String) headers.get("xmpp_to");
        Long timestamp = (Long) headers.get("timestamp");
        String payload = (String) message.getPayload();

        logger.debug("Received a message from: {} with payload: {}", from, payload);
    }
}

      

+2


source







All Articles