我是 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/