简体   繁体   中英

How to send a gcm xmpp message with Spring Integration?

I am new to Spring Integration and to Google Cloud Message. XmppConnectionFactoryBean is created successfully and I can autowire a XmppConnection in my service class.

@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? Should I use XmppConnection directly or some Spring messaging abstraction?

UPDATE

Created a MessageHandler and defined bean names.

@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. It is 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 . So, with the next Spring Integration 4.3 version you will have more GCM support there.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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