【问题标题】:XMPP Spring integration - ChatXMPP Spring 集成 - 聊天
【发布时间】:2017-03-21 14:06:08
【问题描述】:

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

context.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。请指导我做错了什么。我必须使用什么。

【问题讨论】:

    标签: spring-boot xmpp spring-integration smack


    【解决方案1】:

    你用MessageChannel奇怪的方式。

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

    生产者向通道发送消息,消费者从通道接收消息。

    所以,消息通道不能是listener,也不能是sending。

    您只需在配置中声明一对&lt;channel&gt; 并使用适当的&lt;service-activator&gt; 来处理来自XMPP 的消息和其他一些来产生消息。

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

    现在您的 ChatMessageSending 有一个 void 实现,并且没有与目标通道适配器进行任何接线。

    顺便说一句,有一个 XMPP sample 给你。

    【讨论】:

    • 感谢您提供 XMPP 示例链接。我为入站通道适配器和出站通道适配器创建了单独的通道和单独的服务激活器来监听和发送消息。
    猜你喜欢
    • 2018-06-08
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 2018-02-17
    相关资源
    最近更新 更多