[ 
https://issues.apache.org/jira/browse/QPID-7256?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15279316#comment-15279316
 ] 

Steven edited comment on QPID-7256 at 5/11/16 12:48 AM:
--------------------------------------------------------

if I used the following code to test,it can send 1000 message successfully.

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 *
 */


import java.io.InputStream;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


/**
 * After change connection initial
 * @author Steven
 */
public class Hello 
{

        private Connection connection;
        private Context context;
        private Session session;
        
    public Context getContext() {
                return context;
        }

        public void setContext(Context context) {
                this.context = context;
        }

        public Session getSession() {
                return session;
        }

        public void setSession(Session session) {
                this.session = session;
        }

        public Connection getConnection() {
                return connection;
        }

        public void setConnection(Connection connection) {
                this.connection = connection;
        }

        public Hello() 
    {
    }

    public static void main(String[] args)
    {
        Hello hello = new Hello();
        for(int i=0;i<1000;i++){
                hello.runTest();
                System.out.println("message "+i+" sent.");
        }
        hello.closeSession();
        hello.closeConnection();
        hello.closeContext();
    }

    private void runTest() 
    {
        try (InputStream resourceAsStream = 
this.getClass().getResourceAsStream("hello.properties"))
        {
            Properties properties = new Properties();
            properties.load(resourceAsStream);
            if(context == null){
                context = new InitialContext(properties);
            }else{
                context = this.getContext();
            }

            ConnectionFactory connectionFactory = (ConnectionFactory) 
context.lookup("qpidConnectionfactory");
            if(connection == null){
                connection = connectionFactory.createConnection();
            }else{
                connection = this.getConnection();
            }
            connection.start();

            if(session == null){
                session = connection.createSession(false, 
Session.AUTO_ACKNOWLEDGE);
            }else{
                session = this.getSession();
            }
            Queue queue = (Queue) context.lookup("queue");

            MessageProducer messageProducer = session.createProducer(queue);
            
            TextMessage message = session.createTextMessage("Hello world!");
            messageProducer.send(message);
        }
        catch (Exception exp) 
        {
            exp.printStackTrace();
            this.closeSession();
            this.closeConnection();
            this.closeContext();
        }
    }
    
    private void closeConnection(){
        try {
                        connection.close();
                } catch (JMSException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
    }
    
        private void closeContext() {
                try {
                        context.close();
                } catch (NamingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        
        private void closeSession(){
                try {
                        session.close();
                } catch (JMSException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
}




was (Author: stevengu):
if I used the following code to test,it can send 1000 message successfully.

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 *
 */

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * After change connection initial and will throw exception sometimes
 * 
 * @author Steven
 */
public class Hello_BeforeChange {

    public Hello_BeforeChange() {
    }

    public static void main(String[] args) {
        Hello_BeforeChange hello = new Hello_BeforeChange();
        for (int i = 0; i < 1000; i++) {
            hello.runTest();
            System.out.println("message " + i + " sent.");
        }
    }

    private Context getContext() {
        Context context = null;
        try {
            InputStream resourceAsStream = 
this.getClass().getResourceAsStream("hello.properties");
            Properties properties = new Properties();
            properties.load(resourceAsStream);
            context = new InitialContext(properties);
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return context;

    }

    private void runTest() {
        Session session = null;
        Connection connection = null;
        Context context = null;

        try {
            context = this.getContext();
            ConnectionFactory connectionFactory = (ConnectionFactory) 
context.lookup("qpidConnectionfactory");
            connection = connectionFactory.createConnection();
            connection.setExceptionListener(new ExceptionListener()
                {
                        @Override
                        public void onException(JMSException exception)
                        {
                                
System.out.println("ssssssssssssssssssssssssssssssssssssssss");
                        }
                });
            
            connection.start();

            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = (Queue) context.lookup("queue");

            MessageProducer messageProducer = session.createProducer(queue);

            TextMessage message = session.createTextMessage("Hello world!");
            messageProducer.send(message);

        } catch (Exception exp) {

            try {

                if (session != null) {
                    session.close();
                }
                if (connection != null) {
                    connection.stop();
                    connection.close();
                }
            } catch (JMSException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

            exp.printStackTrace();
        }
    }

}


> When invoke the method "connection.start" in a loop,reporting socket closed
> ---------------------------------------------------------------------------
>
>                 Key: QPID-7256
>                 URL: https://issues.apache.org/jira/browse/QPID-7256
>             Project: Qpid
>          Issue Type: Bug
>          Components: Java Client
>    Affects Versions: 0.32
>         Environment: windows7、 jdk7
>            Reporter: Steven
>
> I use the for loop,It will loop 1000 times,every time,I create a 
> connection,then send message,After the message has been sent,close the 
> connection.
> this loop executed it for some times,It will report error,as you can see the 
> screen shot



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to