spring-boot - XMPP Spring 集成 - 聊天

标签 spring-boot xmpp spring-integration smack

我是 XMPP 的新手,试图在 spring-boot 中创建一个服务器来使用 XMPP 监听和发送消息,这就是我尝试过的

上下文.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:int-xmpp="http://www.springframework.org/schema/integration/xmpp"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/integration/xmpp
    http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd">

    <int-xmpp:xmpp-connection
        id="xmppConnection" 
        user="admin@192.168.1.201" 
        password="finatel123" 
        host="192.168.1.201"
        port="5222"/>

    <int-xmpp:inbound-channel-adapter 
        id="xmppInboundAdapter"
        channel="chatMessageListening" 
        xmpp-connection="xmppConnection"
        auto-startup="true"/>

    <int-xmpp:outbound-channel-adapter 
        id="outboundEventAdapter"
        channel="chatMessageSending"
        xmpp-connection="xmppConnection" 
        auto-startup="false">
        <poller fixed-rate="5000" max-messages-per-poll="1"/>
    </int-xmpp:outbound-channel-adapter>

</beans:beans>

ChatMessageListening.java

public class ChatMessageListening implements MessageChannel {

    @Override
    public boolean send(Message<?> message) {

        MessageHeaders headers = message.getHeaders();

        System.out.println("FROM    : "+headers.get(XmppHeaders.FROM));
        System.out.println("TO      : "+headers.get(XmppHeaders.TO));
        System.out.println("Time    : "+new Date((Long)headers.get("timestamp")));

        return true;
    }

    @Override
    public boolean send(Message<?> message, long timeout) {
        return true;
    }
}

ChatMessageSending.java

public class ChatMessageSending implements PollableChannel {

    @Override
    public boolean send(Message<?> message) {
        return true;
    }

    @Override
    public boolean send(Message<?> message, long timeout) {
        return true;
    }

    @Override
    public Message<?> receive() {
        return null;
    }

    @Override
    public Message<?> receive(long timeout) {
            return null;
    }
}

进行此配置后,我能够在 ChatMessageListening.send() 方法中接收消息。

我尝试使用,发送消息,

MessagingTemplate template = new MessagingTemplate();
template.setSendTimeout(5000L);

Message<String> xmppOutboundMsg = MessageBuilder.withPayload("Hello, XMPP!" )
    .setHeader(XmppHeaders.FROM, "admin@192.168.1.201")
    .setHeader(XmppHeaders.TO, "adminone@192.168.1.201")
    .setHeader(XmppHeaders.TYPE, "chat")
    .build();
template.setDefaultChannel(new ChatMessageSending());
template.send(xmppOutboundMsg);

但它不是发送给客户端,而是接收到 ChatMessageSending。请指导我做错了什么。我必须使用什么。

最佳答案

您使用 MessageChannel奇怪的方式。

请考虑修改您对此事的看法: EIP definition和 Spring 集成 implementation :

Producers send Messages to a channel, and consumers receive Messages from a channel.

因此,消息 channel 不能是 listener不能是 sending 这样的东西.

你只需要声明一对 <channel>在您的配置中使用适当的 <service-activator>处理来自 XMPP 的消息和其他一些消息以生成消息。

是的,MessagingTemplate可用于发送,但必须指出正确的MessageChannel bean 你的<int-xmpp:outbound-channel-adapter>将成为订阅者。

现在您的 ChatMessageSending有一个无效的实现,并且没有与目标 channel 适配器的任何接线。

有一个 XMPP sample顺便说一下。

关于spring-boot - XMPP Spring 集成 - 聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42929458/

相关文章:

mysql - 恩弗斯 + MYSQL + 列表 <字符串> = SQLSyntaxErrorException : Specified key was too long;

mysql - MongooseIM 连接到 MySQLDB 未在数据库中存储任何内容

java - Spring-Integration TCP --> eh-cache 链

java - Hibernate 错误地实现了多对多关系?

spring-boot - Spring安全错误循环 View 路径[index] : would dispatch back to the current handler URL [/index] again.检查您的ViewResolver设置

android - 在 ejabberd 上使用 Smack 4.2.0-beta1 创建新用户会抛出 XMPP 异常 : forbidden - auth

java - Spring集成疑问: CRUD operations and sharing events between components

java - NAT 后面的客户端的 Spring Integration UDP 服务器

java - 使用 GIT 的 IDEA 新 gradle spring boot 项目。正确的创建顺序是什么?

javascript - XMPP - 示例第 3 章 "Professional XMPP Programming with JavaScript and jQuery"不起作用