import javax.inject.Inject;
import java.util.HashSet;
import java.util.Set;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
import static org.onap.holmes.common.config.MicroServiceConfig.getMicroServiceIpAndPort;
import static org.onap.holmes.common.utils.CommonUtils.getEnv;
@Service
public class Initializer {
private static final Logger logger = LoggerFactory.getLogger(Initializer.class);
+ private volatile static boolean readyForMsbReg = false;
private MsbRegister msbRegister;
@Inject
@PostConstruct
private void init() {
- try {
- msbRegister.register2Msb(createMicroServiceInfo());
- } catch (CorrelationException e) {
- logger.error(e.getMessage(), e);
+ Executors.newSingleThreadExecutor().execute(() -> {
+ waitUntilReady();
+ try {
+ msbRegister.register2Msb(createMicroServiceInfo());
+ } catch (CorrelationException e) {
+ logger.error(e.getMessage(), e);
+ }
+ });
+ }
+
+ private void waitUntilReady() {
+ int count = 1;
+ while (!readyForMsbReg) {
+ if (count > 20) {
+ break;
+ }
+ int interval = 5 * count++;
+ logger.info("Not ready for MSB registration. Try again after {} seconds...", interval);
+ try {
+ TimeUnit.SECONDS.sleep(interval);
+ } catch (InterruptedException e) {
+ logger.info(e.getMessage(), e);
+ }
}
}
+ public static void setReadyForMsbReg(boolean readyForMsbReg) {
+ Initializer.readyForMsbReg = readyForMsbReg;
+ }
+
private MicroServiceInfo createMicroServiceInfo() {
String[] serviceIpAndPort = getMicroServiceIpAndPort();
MicroServiceInfo msinfo = new MicroServiceInfo();
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.internal.WhiteboxImpl;
+import java.util.concurrent.TimeUnit;
+
@RunWith(PowerMockRunner.class)
@PrepareForTest(MicroServiceConfig.class)
public class InitializerTest {
PowerMock.replayAll();
+ setReadyFlagAfter(3);
+
WhiteboxImpl.invokeMethod(initializer, "init");
+ TimeUnit.SECONDS.sleep(6);
+
PowerMock.verifyAll();
}
+
+ private void setReadyFlagAfter(final int second) {
+ new Thread(() -> {
+ try {
+ TimeUnit.SECONDS.sleep(second);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ Initializer.setReadyForMsbReg(true);
+ }).start();
+ }
}
\ No newline at end of file