【发布时间】: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