如何使用 Spring 集成发送 gcm xmpp 消息?

How to send a gcm xmpp message with Spring Integration?

我是 Spring 集成和 Google 云消息的新手。 XmppConnectionFactoryBean 已成功创建,我可以在我的服务 class.

中自动装配 XmppConnection
@Configuration
class XmppConfig {

    @Value("${gcm.sender_id}")
    private String senderId;

    @Value("${gcm.api_key}")
    private String apiKey;

    @Value("${gcm.host}")
    private String host;

    @Value("${gcm.port}")
    private int port;

    @Bean
    public ConnectionConfiguration connectionConfiguration() {
        ConnectionConfiguration connectionConfig = new ConnectionConfiguration(host, port);
        connectionConfig.setSecurityMode(SecurityMode.enabled);
        connectionConfig.setReconnectionAllowed(true);
        connectionConfig.setRosterLoadedAtLogin(false);
        connectionConfig.setSendPresence(false);
        connectionConfig.setSocketFactory(SSLSocketFactory.getDefault());

        return connectionConfig;
    }

    @Bean
    public XmppConnectionFactoryBean xmppConnectionFactoryBean() {
        XmppConnectionFactoryBean connectionFactoryBean = new XmppConnectionFactoryBean();
        connectionFactoryBean.setUser(senderId);
        connectionFactoryBean.setPassword(apiKey);
        connectionFactoryBean.setConnectionConfiguration(connectionConfiguration());
        return connectionFactoryBean;
    }

}

服务class:

class MyServiceImpl implements MyService {

    @Autowired
    private XmppConnection xmppConnection;

}

这是正确的方法吗?如何向 GCM 发送 XMPP 消息?我应该直接使用 XmppConnection 还是使用一些 Spring 消息传递抽象?

更新

创建了一个 MessageHandler 并定义了 bean 名称。

@Bean(name = "xmppConnection")
public XmppConnectionFactoryBean xmppConnectionFactoryBean() {
    XmppConnectionFactoryBean connectionFactoryBean = new XmppConnectionFactoryBean();
    connectionFactoryBean.setUser(senderId);
    connectionFactoryBean.setPassword(apiKey);
    connectionFactoryBean.setConnectionConfiguration(connectionConfiguration());
    return connectionFactoryBean;
}

@Bean(name = "gcmChannel")
public MessageChannel messageChannel() {
    return new DirectChannel();
}

@Bean
@ServiceActivator(inputChannel = "gcmChannel")
public MessageHandler messageHandler() {
    return new ChatMessageSendingMessageHandler();
}

@Autowired
@Qualifier("gcmChannel")
private MessageChannel messageChannel;

当然,在这件事上使用特定的Spring Integration Adapter 会更好。是ChatMessageSendingMessageHandler.

我们现在正处于 Extensions 对该适配器的支持的合并阶段:https://github.com/spring-projects/spring-integration/pull/1745。因此,在下一个 Spring Integration 4.3 版本中,您将获得更多 GCM 支持。

现在,作为解决方法,您必须手动创建带有 GCM 扩展名的 XMPP 消息并将其发送到 ChatMessageSendingMessageHandler 的频道。