1

EJB 3 と JMS を使用して単純なチャット アプリケーションを作成しています。アプリはほぼ完成しましたが、接続を閉じるのに問題があります。あるユーザーの接続を閉じると、他のユーザーの接続を閉じることができません。これは私のアプリのコードです。

クライアント側

@Stateful
@LocalBean
public class ChatClientBean implements ChatClientLocal{

    private String owner;
    private MessageContainer container = null;

    /* Zmienne połączeniowe */
    private String destination = null;
    private final static String C_FACTORY = "/ConnectionFactory";
    private Context ic;
    private TopicConnectionFactory tcf = null;
    private TopicConnection connection;  
    private Topic topic;
    private TopicSession subSession; 
    private TopicSubscriber subscriber;
    private TopicSession pubSession;
    private TopicPublisher publisher;
    /* -------------------- */

    public ChatClientBean(){}

    @Override
    public void init(String topicName, String owner, MessageContainer container){
        try{
            this.destination = topicName;
            this.owner = owner;
            this.container = container;

            ic = new InitialContext();
            tcf = (TopicConnectionFactory) ic.lookup(C_FACTORY);
            connection = tcf.createTopicConnection();
            pubSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
            subSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
            topic = (Topic) ic.lookup(destination);
            subscriber = subSession.createSubscriber(topic);
            publisher = pubSession.createPublisher(topic);

            subscriber.setMessageListener(this);

            System.out.println("Zaincjalizowano ChatClientBean dla "+owner);
        }catch(JMSException ex){
            System.out.println("Blad przy incjalizacji MessageProccesora");
            ex.printStackTrace();
        }catch(NamingException ex){
            System.out.println("Blad JNDI");
        }
    }

    @Override
    public void onMessage(Message message) {
        try{
                TextMessage tm = (TextMessage) message;
                if(container!= null){
                    container.service(tm.getText());
                }
                System.out.println("Odebrano przez : "+owner+ " : " + tm.getText());

            }catch(JMSException ex){
                System.out.println("Blad JMS przy odbieraniu");
                ex.printStackTrace();
            }
    }

    @Override
    public void sendMessage(String message){
        try{
            TextMessage tm = pubSession.createTextMessage(message);
            publisher.publish(tm);
            System.out.println("Wiadomosc wyslana przez "+owner+" : "+ tm.getText());
        }catch(JMSException ex){
            ex.printStackTrace();
        }
    }

    @Override
    public void start(){
        try{
            connection.start();
        }catch(JMSException ex){
            System.out.println("Blad przy startowaniu polaczenia");
        }
    }

    @Override
    public void close(){
        try{
            if(pubSession != null){
                pubSession.close();
            }
            if(subSession != null){
                subSession.close();
            }
            if(connection != null){
                connection.close();
            }
            if(ic != null){
                ic.close();
            }
        }catch(JMSException ex){
            System.out.println("Blad przy zamykaniu polaczenia : "+ owner);
            ex.printStackTrace();
        }catch(NamingException ex){
            System.out.println("Blad JNDI przy zamykaniu polaczenia "+owner);
        }
    }

    /* GETTERY i SETTERY */
    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public MessageContainer getContainer() {
        return container;
    }

    public void setContainer(MessageContainer container) {
        this.container = container;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }
    /* ----------------- */

オペレーター側

@Stateful
@LocalBean
public class ChatOperatorBean implements ChatOperatorLocal{

    private String owner;
    private ClientsContainer container;

     /* Zmienne połączeniowe */
    private String destination = null;
    private final static String C_FACTORY = "/ConnectionFactory";
    private Context ic;
    private TopicConnectionFactory tcf = null;
    private TopicConnection connection;  
    private Topic topic;
    private TopicSession subSession;
    private TopicSubscriber subscriber;
    private TopicSession pubSession; 
    private TopicPublisher publisher;
    /* -------------------- */

    @Override
    public void init(String topicName, String owner, ClientsContainer container){
        try{
            this.destination = topicName;
            this.owner = owner;
            this.container = container;

            ic = new InitialContext();
            tcf = (TopicConnectionFactory) ic.lookup(C_FACTORY);
            connection = tcf.createTopicConnection();
            pubSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
            subSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
            topic = (Topic) ic.lookup(destination);
            subscriber = subSession.createSubscriber(topic);
            publisher = pubSession.createPublisher(topic);

            subscriber.setMessageListener(this);

            System.out.println("Zaincjalizowano ChatOperatorBean dla "+owner);
        }catch(JMSException ex){
            System.out.println("Blad przy incjalizacji MessageProccesora : "+owner);
            ex.printStackTrace();
        }catch(NamingException ex){
            System.out.println("Blad JNDI : "+owner);
            ex.printStackTrace();
        }
    }

    @Override
    public void onMessage(Message message) {
        try{
            TextMessage tm = (TextMessage) message;
            if(container!= null){
                container.service(tm.getText());
            }
            System.out.println("Odebrano przez : "+owner+" : "+ tm.getText());
        }catch(JMSException ex){
            System.out.println("Blad JMS przy odbieraniu u : "+owner);
            ex.printStackTrace();
        }
    }

    @Override
    public void sendMessage(String message){
        try{
            TextMessage tm = pubSession.createTextMessage(message);
            publisher.publish(tm);
            System.out.println("Wiadomosc wyslana przez "+owner+" : "+ tm.getText());
        }catch(JMSException ex){
            ex.printStackTrace();
        }
    }

    public void start(){
        try{
            connection.start();
        }catch(JMSException ex){
            System.out.println("Blad przy startowaniu polaczenia : "+owner);
        }
    }

    @Override
    public void close(){
        try{
            if(pubSession != null){
                pubSession.close();
            }
            if(subSession != null){
                subSession.close();
            }
            if(connection != null){
                connection.close();
            }
            if(ic != null){
                ic.close();
            }
        }catch(JMSException ex){
            System.out.println("Blad przy zamykaniu polaczenia : "+ owner);
            ex.printStackTrace();
        }catch(NamingException ex){
            System.out.println("Blad JNDI przy zamykaniu polaczenia "+owner);
        }
    }

    /* GETTERY i SETTERY */
    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public ClientsContainer getContainer() {
        return container;
    }

    public void setContainer(ClientsContainer container) {
        this.container = container;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }
    /* ----------------- */

client.xhtml

<h:body style="background-color: #99ff66">
        <h:outputStylesheet name="css/default.css" /> 
        <h:form id="form" >
            <p:outputPanel id="panel" >
                <h:panelGrid columns="2" cellpadding="5" id="startPanel" rendered="#{client.startPanelVisible}"  >
                                <h:outputLabel value="Nazwa" for="nick" style="font-weight: bold;" />
                                <p:inputText value="#{client.nick}" id="nick" />
                                <p:commandButton value="Rozpocznij" actionListener="#{client.startChat()}" process="@this,nick" update="panel" id="start"/> 

                </h:panelGrid>
                <p:panel header="Wiadomości" id="chatboxPanel" rendered="#{client.chatPanelVisible}">
                    <h:panelGroup id="chatbox">
                        <ui:repeat value="#{client.container.messages}" var="item">
                            #{item}
                            <br/>
                            <p:separator/>
                        </ui:repeat>
                        <p:poll interval="1" update="chatbox" />
                    </h:panelGroup>
                </p:panel>
                <p:outputPanel id="chatPanel" rendered="#{client.chatPanelVisible}">
                    <p:inputText value="#{client.message}" 
                                 styleClass="messageInput" 
                                 style="margin-top: 5px;width: 310px;"
                                 />
                    <p:commandButton value="Send" 
                                     actionListener="#{client.send()}" 
                                     oncomplete="$('.messageInput').val('').focus()" 
                                     />
                    <p:commandButton actionListener="#{client.closeChat()}" process="@this" update="panel"
                                     value="Close" 
                                     />
                </p:outputPanel>
            </p:outputPanel>
        </h:form>
    </h:body>

operator.xhtml

<h:body>
        <h:outputStylesheet name="css/defaultOperator.css" />
        Zalogowany jako : #{logOn.operator.firstName} #{logOn.operator.lastName}
        <p:separator />
        <h:form id="form" prependId="false">
                <p:selectOneButton value="#{operator.viewController.selectedPanel}"  style="margin-bottom: 10px;" >
                    <f:selectItem itemValue="0" itemLabel="Pierwszy" />
                    <f:selectItem itemValue="1" itemLabel="Kod na stronę" />
                    <f:selectItem itemValue="2" itemLabel="Czaty" />
                    <p:ajax event="change" update="form" />
                </p:selectOneButton>

                <p:outputPanel id="panel0" rendered="#{operator.viewController.getVisible(0)}">
                </p:outputPanel>
                <p:outputPanel id="panel1" rendered="#{operator.viewController.getVisible(1)}">
                    <p:fieldset styleClass="fieldset">
                        <h:panelGrid columns="1" >
                            <h:outputText value="Wklej ten kod w sekcji head strony" />
                            <h:inputTextarea id="trackingCode" readonly="true" rows="10" cols="40" value="#{operator.tr}"/>
                        </h:panelGrid>
                    </p:fieldset>
                </p:outputPanel>
                <p:outputPanel id="panel2" rendered="#{operator.viewController.getVisible(2)}" autoUpdate="true">
                    <p:outputPanel styleClass="left">
                        <p:panel header="Status" styleClass="statusBox">
                            <p:commandButton actionListener="#{operator.startChat()}" value="Połącz" />
                            <p:commandButton actionListener="#{operator.closeChat()}" value="Rozłącz" />
                        </p:panel>
                        <p:panel header="Oczekujący klienci" styleClass="clientBox">
                            <p:outputPanel id="clientPanel" >
                                <ui:repeat value="#{operator.container.notServiced}"  var="item" >
                                    <h:outputText value="#{item.clientName}" />
                                    <p:commandButton styleClass="startbtn" value=">>" actionListener="#{operator.startWithClient(item.clientName)}" />
                                    <p:separator />
                                </ui:repeat>
                            </p:outputPanel>
                        </p:panel>
                    </p:outputPanel>
                    <p:panel header="Aktywne chaty" id="activeChats" styleClass="chatTabs">
                        <p:tabView id="tabView" value="#{operator.container.serviced}" var="item">
                            <p:tab id="clientTab" title="#{item.clientName}" >
                               <p:outputPanel >
                                    <p:panel header="Wiadomości" id="chatboxPanel" styleClass="messageBox">
                                        <p:outputPanel id="chatbox" >
                                             <ui:repeat value="#{item.messages}" var="msg">
                                                 <b>#{msg.from} : </b>#{msg.text}
                                                 <p:separator />
                                             </ui:repeat>
                                        </p:outputPanel>

                                     </p:panel>
                                   <p:poll interval="1" update="chatbox" />
                                </p:outputPanel>
                                <p:outputPanel id="chatPanel" >
                                    <p:inputText value="#{item.currentMessage}" 
                                             styleClass="messageInput" 
                                             style="margin-top: 5px;width: 310px;"
                                             />
                                    <p:commandButton value="Send" 
                                                 actionListener="#{operator.send(item.clientName,item.currentMessage)}" 
                                                 oncomplete="$('.messageInput').val('').focus()" 
                                                 styleClass="button"
                                                 />
                                </p:outputPanel>
                            </p:tab>
                        </p:tabView>
                    </p:panel>
                </p:outputPanel>
            </h:form>
    </h:body>

助けてください!!!

4

0 に答える 0