简体   繁体   English

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

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

I am new to Spring Integration and to Google Cloud Message.我是 Spring Integration 和 Google Cloud Message 的新手。 XmppConnectionFactoryBean is created successfully and I can autowire a XmppConnection in my service class. 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;
    }

}

Service class:服务等级:

class MyServiceImpl implements MyService {

    @Autowired
    private XmppConnection xmppConnection;

}

Is that the right approach?这是正确的方法吗? How can I send an XMPP message to GCM?如何向 GCM 发送 XMPP 消息? Should I use XmppConnection directly or some Spring messaging abstraction?我应该直接使用 XmppConnection 还是某些 Spring 消息传递抽象?

UPDATE更新

Created a MessageHandler and defined bean names.创建了一个 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;

Of course, it would be better to use a specific Spring Integration Adapter on the matter.当然,在这件事上使用特定的 Spring Integration Adapter 会更好。 It is ChatMessageSendingMessageHandler .它是ChatMessageSendingMessageHandler

And we right now in the merging phase for the Extensions support for that adapter:https://github.com/spring-projects/spring-integration/pull/1745 .我们现在正处于该适配器的Extensions支持的合并阶段:https ://github.com/spring-projects/spring-integration/pull/1745 So, with the next Spring Integration 4.3 version you will have more GCM support there.因此,在下一个 Spring Integration 4.3 版本中,您将在那里获得更多 GCM 支持。

Right now as a workaround you have to create an XMPP message with GCM extension manually and send it to the channel for that ChatMessageSendingMessageHandler .现在作为一种解决方法,您必须手动创建带有 GCM 扩展的 XMPP 消息,并将其发送到该ChatMessageSendingMessageHandler的频道。

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

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