com.wl4g.infra.common.tests.integration.testcontainers.RocketMQContainer Maven / Gradle / Ivy
/**
* Copyright (C) 2023 ~ 2035 the original authors WL4G (James Wong).
*
* 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
*
* 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.
*
*/
package com.wl4g.infra.common.tests.integration.testcontainers;
import com.github.dockerjava.api.command.InspectContainerResponse;
import lombok.SneakyThrows;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;
import java.util.ArrayList;
import java.util.List;
/**
* We not use the FixedHostPortGenericContainer here, because the multiple instances often exist on the same machine.
*
* @see github RocketMQContainer.java
*/
public class RocketMQContainer extends GenericContainer {
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("apache/rocketmq");
private static final String DEFAULT_TAG = "4.9.4";
// READ and WRITE
private static final int defaultBrokerPermission = 6;
public static final int NAMESRV_PORT = 9876;
public static final int BROKER_PORT = 10911;
@Deprecated
public RocketMQContainer() {
this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_TAG));
}
@Deprecated
public RocketMQContainer(String rocketVersion) {
this(DEFAULT_IMAGE_NAME.withTag(rocketVersion));
}
public RocketMQContainer(final DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
withExposedPorts(NAMESRV_PORT, BROKER_PORT, BROKER_PORT - 2);
}
@Override
protected void configure() {
String command = "#!/bin/bash\n";
command += "./mqnamesrv &\n";
command += "./mqbroker -n localhost:" + NAMESRV_PORT;
withCommand("sh", "-c", command);
}
@Override
@SneakyThrows
protected void containerIsStarted(InspectContainerResponse containerInfo) {
List updateBrokerConfigCommands = new ArrayList<>();
// Update the brokerAddr and the clients can use the mapped address to connect the broker.
updateBrokerConfigCommands.add(updateBrokerConfig("brokerIP1", getHost()));
updateBrokerConfigCommands.add(updateBrokerConfig("listenPort", getMappedPort(BROKER_PORT)));
// Make the changes take effect immediately.
updateBrokerConfigCommands.add(updateBrokerConfig("brokerPermission", defaultBrokerPermission));
final String command = String.join(" && ", updateBrokerConfigCommands);
try {
final ExecResult result = execInContainer(
"/bin/sh",
"-c",
command
);
if (result.getExitCode() != 0) {
throw new IllegalStateException(result.toString());
}
} catch (Throwable ex) {
throw new IllegalStateException(ex);
}
}
private String updateBrokerConfig(final String key, final Object val) {
final String brokerAddr = "localhost:" + BROKER_PORT;
return "./mqadmin updateBrokerConfig -b " + brokerAddr + " -k " + key + " -v " + val;
}
public String getNamesrvAddr() {
return String.format("%s:%s", getHost(), getMappedPort(NAMESRV_PORT));
}
}