2015-10-26 1 views

Répondre

2

Cette solution fonctionne pour la discussion "simple" comme vous l'avez expliqué.

Il n'y a pas beaucoup d'informations sur la façon dont vous avez construit cela auparavant, donc je vais juste expliquer comment avoir un bean étendu Application qui peut être injecté dans d'autres beans pour gérer le chat de stockage.

Vous pouvez configurer un service pour stocker ces informations.

ChatHistoryService.java

@Service 
@Scope("application")//This is the key this will keep the chatHistory alive for the length of the running application(As long as you don't have multiple instances deployed(But as you said it's simple so it shouldn't) 
public class ChatHistoryService { 

    List<String> chatHistory = new LinkedList<>();//Use LinkedList to maintain order of input 

    public void storeChatHistory(String chatString) { 
     chatHistory.add(chatString); 
    } 

    public List<String> getChatHistory() { 
     //I would highly suggest creating a defensive copy of the chat here so it can't be modified. 
     return Collections.unmodifiableList(chatHistory); 
    } 

} 

YourChatController.java

@Controller 
public class YourChatController { 

    @Autowired 
    ChatHistoryService historyService; 

    ...I'm assuming you already have chat logic but you aren't storing the chat here is where that would go 

    ...When chat comes in call historyService.storeChatHistory(chatMessage); 

    ...When you want your chat call historyService.getChatHistory(); 

} 

Encore une fois garder à l'esprit que cela ne fonctionne vraiment que pour une application simple. Si elle est distribuée il y aura différentes histoires de chat par instance de l'application à ce moment-là vous pourriez regarder dans un cache distribué.

Dans tous les cas, ne pas aller au-delà simple avec cette implémentation.

Si vous regardez ici, il vous donnera une idée de plusieurs caches qui fonctionnent avec la botte à ressort.

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache