【问题标题】:How to send a gcm xmpp message with Spring Integration?如何使用 Spring Integration 发送 gcm xmpp 消息?
【发布时间】:2016-04-01 16:44:25
【问题描述】:

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

【问题讨论】:

    标签: java spring google-cloud-messaging xmpp spring-integration


    【解决方案1】:

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

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

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

    【讨论】:

    • 很好,感谢您的帮助。有什么方法可以在没有 xml 的情况下创建所有需要的东西?在我的服务类中,我应该使用 MessageChannel 实例吗?
    • M-m-m。是的,您必须为 ChatMessageSendingMessageHandler 和一些 MessageChannel bean 声明 @ServiceActivator 才能与之交互。当您可以从@MessagingGateway 功能中获得收益时,我不确定您为什么需要MyService。您可以在 Spring 集成手册中找到所有内容。
    • 好的。看看答案更新。现在我需要创建一个 AbstractTransformer 来将 Spring 消息转换为带有 gcm 有效负载的 Smack 消息?
    • 配置看起来不错,但不要忘记添加@EnableIntegration。不,你不需要这样的变压器。您只需提供Smack message 作为payload 即可发送。这可以作为@Gateway 的参数。
    猜你喜欢
    • 2019-06-08
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 2011-05-18
    • 2018-11-24
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多