繁体   English   中英

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

[英]How to send a gcm xmpp message with Spring Integration?

我是 Spring Integration 和 Google Cloud Message 的新手。 XmppConnectionFactoryBean已成功创建,我可以在我的服务类中自动装配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 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的频道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM