What are the right parameters for xmpp-connection

2019-08-11 04:02发布

I want to setup spring-integration-xmpp on my spring app to make it receive upstream messages from android devices. I can already send messages to the android device using http but I cannot set up the xmpp-connection bean so it gives me:

failed to connect to gcm-preprod.googleapis.com; nested exception is Connection failed. No response from server.:

This is my spring integration configuration:

  <int:channel id="gcmOutboundNotificationChannel"/>

  <int-xmpp:xmpp-connection
    id="xmppConnection"
    user="${tracker.server.app.id}@gcm.googleapis.com"
    password="${tracker.auth.key}"
    host="gcm-preprod.googleapis.com"
    port="5236"
    subscription-mode="accept_all"/>

  <int-xmpp:outbound-channel-adapter
    id="gcmOutboundAdapter"
    xmpp-connection="xmppConnection"
    channel="gcmOutboundNotificationChannel"/>

tracker.server.app.id is a 12 digit number and tracker.auth.key is like AIzaSyBdfZ4oBaVuu07sjW5e9DnogeUF6NV**** (I put in the asterisks).

What am I missing?

2条回答
淡お忘
2楼-- · 2019-08-11 04:56

I have configured the xmpp connection as a bean like this:

@Configuration
public class GcmXmppConnection {

@Value("${gcm.senderID}")
private String username;

@Value("${gcm.apiKey}")
private String password;

@Value("${gcm.host}")
private String host;

@Value("${gcm.port}")
private int port;

@Bean(name="gcmConnection")
public XmppConnectionFactoryBean xmppConnectionFactoryBean(){

    ConnectionConfiguration configuration = new    ConnectionConfiguration(host, port);
    configuration.setSecurityMode(SecurityMode.enabled);
    configuration.setReconnectionAllowed(true);
    configuration.setRosterLoadedAtLogin(false);
    configuration.setSendPresence(false);
    configuration.setSocketFactory(SSLSocketFactory.getDefault());

//      configuration.setDebuggerEnabled(true);
    XmppConnectionFactoryBean connectionFactoryBean = new XmppConnectionFactoryBean(configuration);

    connectionFactoryBean.setUser(username);
    connectionFactoryBean.setPassword(password);

    return connectionFactoryBean;
}
}

The configuration is autowired in the xml configuration like this:

<!-- Outbound messages to gcm -->
    <int:chain input-channel="androidNotificationOutputChannel">
    <int:transformer ref="androidMessageTransformer"></int:transformer>
    <int-xmpp:outbound-channel-adapter xmpp-connection="gcmConnection"/>
</int:chain>

<!-- Inbound messages from gcm -->
<int:channel id="gcmInboundNotificationChannel"/>
<int-xmpp:inbound-channel-adapter id="gcmInboundAdapter"
    channel="gcmInboundNotificationChannel" xmpp-connection="gcmConnection"
    extract-payload="true" auto-startup="true" />

The last piece is androidMessageTransformer, it's pretty simple, like the gcmXmppConnection bean it was coded along the example in google documentation.

@MessageEndpoint
public class AndroidMessageTransformer extends AbstractTransformer {

public final static String DESTINATION_HEADER_KEY="push.destinationID";

private final static String MESSAGE_ID_FORMAT = "%s-%s";

@Value("${gcm.senderID}")
private String senderId;

@Autowired
ObjectMapper om;

@SuppressWarnings("unchecked")
@Override
protected Object doTransform(Message<?> msgIn) throws Exception {
    Map<String,String> data = (Map<String, String>) msgIn.getPayload();
    String registrationID = msgIn.getHeaders().get(DESTINATION_HEADER_KEY,String.class);
    Map<String, Object> gcmPayload = new HashMap<>();

    gcmPayload.put("to", registrationID);
    gcmPayload.put("message_id", String.format(Locale.ENGLISH, MESSAGE_ID_FORMAT, senderId, UUID.randomUUID().toString()));
    gcmPayload.put("data", data);

    String gcmJsonPayload = om.writeValueAsString(gcmPayload);

    org.jivesoftware.smack.packet.Message xmppMessage = new org.jivesoftware.smack.packet.Message();
    xmppMessage.addExtension(new GcmPacketExtension(gcmJsonPayload));

    return xmppMessage;
}
}

This works reliably for me, although I have mostly worked with the outbound direction, and never checked much about the inbound side.

查看更多
Lonely孤独者°
3楼-- · 2019-08-11 05:03

I think you are missing auto-startup="true" in the <int-xmpp:outbound-channel-adapter tag. Refer to the following answer might work for you.

查看更多
登录 后发表回答