Search code examples
javaamqpqpid

Validate an AMQConnection connection string without connecting?


The Apache Qpid Java client API has an AMQConnection class that is used to make a connection to a Qpid message broker. I'm using the single-String constructor (AMQConnection(String connection)). I have a utility method that creates the connection string first, which is then passed to the AMQConnection constructor.

The connection string is of the form amqp://<username>:<password>@<clientID>/?brokerlist='tcp://<hostname>:<port>'.

If the constructor is passed a connection string with incorrect syntax, it throws a URLSyntaxException.

I would like to move the connection string syntax check to the utility method (I'm of the opinion that it shouldn't be able to return broken information), but I have not found any way of validating the connection string short of trying to set up a connection to the message broker. Is there any way of doing this, or do I just have to rely on the URLSyntaxException being thrown from the constructor?


Solution

  • Do you only want to check the URI for correct syntax? If so, simply create an instance of URI class:

    new URI("amqp://user:pwd@42/?brokerlist='tcp://example.com:80'")
    

    This throws an java.net.URISyntaxException when URI is not syntactically correct.

    If you need to check the URI for AMQ-specific options, you might have two have a look at AMQ source code and find a class that validates/parses this.