You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@qpid.apache.org by "Steven (JIRA)" <ji...@apache.org> on 2016/05/20 01:28:12 UTC

[jira] [Commented] (QPID-7275) connection.setExceptionListener() method cannot be invoked for Qpid

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

Steven commented on QPID-7275:
------------------------------

below is my java application code:

/*
 *
 * 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;

/**
 * AMQP SendMessage Reconnect
 * 
 * @author Steven
 */
public class AMQPSendMessageReconnect implements ExceptionListener {
    public Connection getConnection() {
	return connection;
    }

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

    private Connection connection;
    int retry_times = 3;
    int retry_time = 3000;

    public AMQPSendMessageReconnect() {
    }

    public static void main(String[] args) {
	AMQPSendMessageReconnect hello = new AMQPSendMessageReconnect();
	hello.runTest();
	System.out.println("message has been sent.");
    }

    private Context getContext() {
	InitialContext 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;
	Context context = null;
	try (InputStream resourceAsStream = this.getClass().getResourceAsStream("hello.properties")) {
	    Properties properties = new Properties();
	    properties.load(resourceAsStream);
	    context = new InitialContext(properties);

	    ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("qpidConnectionfactory");
	    connection = connectionFactory.createConnection();
	    connection.setExceptionListener(this);
	    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 (IOException e1) {
	    // TODO Auto-generated catch block
	    e1.printStackTrace();
	} catch (NamingException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (JMSException e1) {
	    // TODO Auto-generated catch block
	    onException(e1);
	}catch(Throwable e1){
	    
	}
	finally {
	    try {
		if (session != null) {
		    session.close();
		}
		if (connection != null) {
		    connection.stop();
		    connection.close();
		}
	    } catch (JMSException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }

	}
    }

    @Override
    public void onException(JMSException paramJMSException) {
	// assume disconnected
	InitialContext context = null;
	ConnectionFactory connectionFactory = null;
	Session session = null;
	if (connection != null) {
	    try {
		connection.close();
	    } catch (JMSException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	    int attemptNumber = 1;
	    while (attemptNumber <= retry_times) {
		try {
		    Thread.sleep(retry_time);
		} catch (InterruptedException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
		try {
		    context = (InitialContext) getContext();
		    connectionFactory = (ConnectionFactory) context.lookup("qpidConnectionfactory");
		    connection = connectionFactory.createConnection();
		    connection.setExceptionListener(this);
		    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);
		    break;
		} catch (JMSException e) {
		    // TODO Auto-generated catch block
		    attemptNumber++;
		    // e.printStackTrace();
		} catch (NamingException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}

	    }
	}

    }

}


> connection.setExceptionListener() method cannot be invoked for Qpid
> -------------------------------------------------------------------
>
>                 Key: QPID-7275
>                 URL: https://issues.apache.org/jira/browse/QPID-7275
>             Project: Qpid
>          Issue Type: Bug
>          Components: Java Client
>    Affects Versions: 0.32
>         Environment: Window7,jdk7
>            Reporter: Steven
>
> I want to implement Re-connection functionality for AMQP 1.0,as We all know that AMQP 1.0 protocol 's connection URL didn't support "broker-list",Hence,I want to register the method "connection.setExceptionListener"  to monitor the connection between client socket and server socket before invoking the method "connection.start" ,I try to mock the connection closed through the three following way:
> 1.stop the Qid server broker
> 2.pull out the cable
> 3.use the TCPViewer to close the connection manually.
> none of the above method didn't invoke the method "connection.setExceptionListener"
> by the way,for the client API qpid-amqp-1-0-client-0.32,If  the connection URL support brokerlist feature,That would be nice,I can implement Re-connection functionality in a nice way,Thanks in advance.



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

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@qpid.apache.org
For additional commands, e-mail: dev-help@qpid.apache.org