public static String proccesClientId(String username) {
long now = new Date().getTime();
// Check for available channel
log.info("begin query");
ChannelConnection availableChannel = ObjectifyUtil.ofy().load()
.type(ChannelConnection.class)
.filter("active", false)
.filter("expires >", now + FIVE_MINUTES)
.first().get();
if (availableChannel != null) {
log.info("found available channel " + availableChannel.getChannelToken());
availableChannel.setUsername(username).save();
return availableChannel.getChannelToken();
} else {
// Generate new client id
log.info("gen rand");
String newClientId = new BigInteger(130, rand).toString(32);
ChannelService channelService = ChannelServiceFactory.getChannelService();
// Open new channel
log.info("duration minutes: " + CHANNEL_TOKEN_TIMEOUT_MINUTES);
String newChannelToken = channelService.createChannel(newClientId, CHANNEL_TOKEN_TIMEOUT_MINUTES);
// Save new entity
long expires = new Date().getTime() + CHANNEL_TOKEN_TIMEOUT_MILIS;
new ChannelConnection()
.setClientId(newClientId)
.setChannelToken(newChannelToken)
.setExpires(expires)
.setActive(false)
.setUsername(username)