0.8 spec holdover -- "Unable to publish in detached, failed or suspended state"
https://github.com/ably/ably-java/blob/2af72fb88e0d5e0b47820c1c1cab6a2235f5df73/lib/src/main/java/io/ably/lib/realtime/Channel.java#L747-L751
This was correct for 0.8, and is still correct for failed or suspended, but in the 1.0 spec, publishing while detached should trigger an implicit attach (for 1.0 spec initial version) or just publish anyway (for spec as updated). https://docs.ably.io/client-lib-development-guide/features/#RTL6c1
We're experiencing this bug intermittently with 1.0.7. When trying to publish we occasionally get:
java.lang.Exception: Unable to publish in detached, failed or suspended state
I've put in a workaround like this to see if it helps:
private void publish(String channelName, String type, Object payload) {
try {
final String json = objectMapper.writeValueAsString(payload);
final Channel channel = ably.channels.get(channelName);
if (channel.state == ChannelState.detached || channel.state == ChannelState.detaching) {
channel.attach(new CompletionListener() {
@Override
public void onSuccess() {
try {
channel.publish(type, json);
lastPublishedTimes.put(channelName, System.currentTimeMillis());
} catch (AblyException e) {
log.error("Failed to publish message to Ably channel {} after reattaching", channelName, e);
}
}
@Override
public void onError(ErrorInfo reason) {
log.error("Failed to reattach Ably channel: {}", reason);
}
});
} else {
channel.publish(type, json);
lastPublishedTimes.put(channelName, System.currentTimeMillis());
}
} catch (Exception e) {
log.error("Failed to publish message to Ably channel {}", channelName, e);
}
I suspect the problem is occurring because we have a reaper job that releases idle Ably channels after they've been inactive for a while. If someone tries to publish again while the channel is in the process of being released, this happens.
I'm increasing the timeout and applying the above workaround in hopes that it works, but it would be a lot better if ably-java implicitly reattached per the 1.0 spec.