Add ConnectionDetail support to Rabbit auto-configuration
Update Rabbit auto-configuration so that `RabbitConnectionDetails` beans may be optionally used to provide connection details. See gh-34657 Co-Authored-By: Mortitz Halbritter <mkammerer@vmware.com> Co-Authored-By: Phillip Webb <pwebb@vmware.com>pull/34759/head
parent
de8fb04814
commit
69f31cb6c0
@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.autoconfigure.amqp;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adapts {@link RabbitProperties} to {@link RabbitConnectionDetails}.
|
||||||
|
*
|
||||||
|
* @author Moritz Halbritter
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
* @author Phillip Webb
|
||||||
|
* @since 3.1.0
|
||||||
|
*/
|
||||||
|
public class PropertiesRabbitConnectionDetails implements RabbitConnectionDetails {
|
||||||
|
|
||||||
|
private final RabbitProperties properties;
|
||||||
|
|
||||||
|
public PropertiesRabbitConnectionDetails(RabbitProperties properties) {
|
||||||
|
this.properties = properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUsername() {
|
||||||
|
return this.properties.determineUsername();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPassword() {
|
||||||
|
return this.properties.determinePassword();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getVirtualHost() {
|
||||||
|
return this.properties.determineVirtualHost();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Address> getAddresses() {
|
||||||
|
List<Address> addresses = new ArrayList<>();
|
||||||
|
for (String address : this.properties.determineAddresses().split(",")) {
|
||||||
|
String[] components = address.split(":");
|
||||||
|
addresses.add(new Address(components[0], Integer.parseInt(components[1])));
|
||||||
|
}
|
||||||
|
return addresses;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.autoconfigure.amqp;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Details required to establish a connection to a RabbitMQ service.
|
||||||
|
*
|
||||||
|
* @author Moritz Halbritter
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
* @author Phillip Webb
|
||||||
|
* @since 3.1.0
|
||||||
|
*/
|
||||||
|
public interface RabbitConnectionDetails extends ConnectionDetails {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Login user to authenticate to the broker.
|
||||||
|
* @return the login user to authenticate to the broker or {@code null}
|
||||||
|
*/
|
||||||
|
default String getUsername() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Login to authenticate against the broker.
|
||||||
|
* @return the login to authenticate against the broker or {@code null}
|
||||||
|
*/
|
||||||
|
default String getPassword() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Virtual host to use when connecting to the broker.
|
||||||
|
* @return the virtual host to use when connecting to the broker or {@code null}
|
||||||
|
*/
|
||||||
|
default String getVirtualHost() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of addresses to which the client should connect. Must return at least one
|
||||||
|
* address.
|
||||||
|
* @return the list of addresses to which the client should connect
|
||||||
|
*/
|
||||||
|
List<Address> getAddresses();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the first address.
|
||||||
|
* @return the first address
|
||||||
|
* @throws IllegalStateException if the address list is empty
|
||||||
|
*/
|
||||||
|
default Address getFirstAddress() {
|
||||||
|
List<Address> addresses = getAddresses();
|
||||||
|
Assert.state(!addresses.isEmpty(), "Address list is empty");
|
||||||
|
return addresses.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A RabbitMQ address.
|
||||||
|
*
|
||||||
|
* @param host the host
|
||||||
|
* @param port the port
|
||||||
|
*/
|
||||||
|
record Address(String host, int port) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue