如何接收/处理发送给运行Spring Boot / Integration的用户的XMPP消息

时间:2020-12-08 19:54:22

Currently i have a runnning Spring Boot application that logs in into a XMPP server as a specific user. I can send messages to remote clients etc. But now i want to receive messages on the serverside if clients are sending messages to that specific user that is logged in via Spring Boot.

目前我有一个运行的Spring Boot应用程序,它作为特定用户登录到XMPP服务器。我可以向远程客户端等发送消息。但是现在我想在服务器端接收消息,如果客户端正在向通过Spring Boot登录的特定用户发送消息。

I tried to hookup a service activator that actually receives a message but the message content is put into the "to" field and the "from" property is null. So i am puzzled on how to hook up things..

我尝试连接实际接收消息的服务激活器,但消息内容被放入“to”字段,“from”属性为null。所以我很困惑如何挂钩的东西..

Below is the xmpp configuration of the incoming message adapter:

以下是传入消息适配器的xmpp配置:

<!-- 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 the 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 send a message to the server the output of:

所以当我向服务器发送消息时输出:

  • System.out.println(message.getFrom()); is null
  • 的System.out.println(message.getFrom());一片空白

  • System.out.println(message.getTo()); is the actual message content
  • 的System.out.println(message.getTo());是实际的消息内容

  • System.out.println(message.getBody()); is null
  • 的System.out.println(message.getBody());一片空白

What am i missing overhere?

我错过了什么?

1 个解决方案

#1


Solution to this problem was to use the correct Message implementation!

解决这个问题的方法是使用正确的Message实现!

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);
    }
}

#1


Solution to this problem was to use the correct Message implementation!

解决这个问题的方法是使用正确的Message实现!

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);
    }
}