/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.async.chat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.StaleAsyncWebRequestException;
/**
* A ChatParticipant represents a user participating in a chat on a specific topic.
*
* <p>A participant in a chat makes polling requests continuously and waits until
* a message arrives or the request times out. At the start of each polling
* request a DeferredResult is created which can be used to process a chat message
* as soon as it arrives (see {@link #processMessage(ChatMessage)}. In addition,
* an internal cache is used to save messages that arrive after one polling
* request ends and before the next one arrives.
*/
public class ChatParticipant {
private final String user;
private final String topic;
private final List<ChatMessage> messageCache = new ArrayList<ChatMessage>();
private DeferredResult deferredResult;
private final Object lock = new Object();
/**
* Create a ChatParticipant instance.
*/
public ChatParticipant(String user, String topic) {
this.user = user;
this.topic = topic;
}
public String getUser() {
return user;
}
public String getTopic() {
return topic;
}
/**
* Process the message by setting the DeferredResult or if a DeferredResult
* is not available (between polling requests?), save the message.
*
* <p>Note: this method is thread-safe since some threads may poll for new
* messages while other threads post messages.
*/
public void processMessage(ChatMessage message) {
synchronized (lock) {
if (this.deferredResult != null) {
try {
this.deferredResult.set(Arrays.asList(message));
return;
}
catch (StaleAsyncWebRequestException e) {
// fall through and save message
System.out.println("times out");
}
finally {
this.deferredResult = null;
}
}
this.messageCache.add(message);
}
}
/**
* Return saved messages or if none are available return {@code null} and save
* the DeferredResult to process the messages when they become available.
*
* <p>Note: this method is thread-safe since some threads may poll for new
* messages while other threads post messages.
*/
public List<ChatMessage> getMessagesWhenAvailable(DeferredResult deferredResult) {
synchronized(lock) {
if (this.messageCache.isEmpty()) {
this.deferredResult = deferredResult;
return null;
}
else {
ArrayList<ChatMessage> result = new ArrayList<ChatMessage>(this.messageCache);
this.messageCache.clear();
return result;
}
}
}
@Override
public String toString() {
return "ChatParticipant [user=" + user + ", topic=" + topic + "]";
}
}