0

I am writing a chat application using java websocket, now I need to save chat history, so when calling onMessage method I create my object, set its property and save it to data base,but with no reason It doesn't recieve to persist part, here is my code:

@OnMessage
public void onMessage(final Session session,
        final ChatMessageBean chatMessage) {
    String sender = (String) session.getUserProperties().get("user");

    try {

        for (Session s : session.getOpenSessions()) {
            if (s.isOpen()
                    && ((chatMessage.getReceiver()).equals(s
                            .getUserProperties().get("user")) || sender
                            .equals(s.getUserProperties().get("user")))) {
                s.getBasicRemote().sendObject(chatMessage);

                System.err.println("chatMessage is : " + chatMessage);
                System.err.println("user is : "
                        + s.getUserProperties().get("user"));
            }

        }

    } catch (IOException | EncodeException e) {
        log.log(Level.WARNING, "onMessage failed", e);

    }
    ChatMessage chatConv = new ChatMessage();
    chatConv.setMessage("hihii");
    chatConv.setRecievedDate(new Date());
    chatConv.setSeenStaus(true);
    chatConv.setSenderId("7923");
    chatConv.setReciever("702");
    chatCrud.addChat(chatConv);

}

and my addChat method on chatCrud Class:

public class ChatCrud {

@Inject
EntityManager em;

public String addChat(ChatMessage chat) {
    try{
    em.getTransaction().begin();
    em.persist(chat);
    em.getTransaction().commit();
    }catch(Exception e){
        System.out.println("!!!!!! "+e);
    }
    return "OK";
}
   }

I changed my method to catch exception, The sysout result was:

!!!!!! org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.RequestScoped

Is onMessage method the wrong place to save history of chat?

Nazila
  • 1,353
  • 4
  • 15
  • 28

0 Answers0