Skip to content

Commit

Permalink
Cleaner run-once semantics.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed May 8, 2016
1 parent 030e638 commit 94c6919
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 28 deletions.
Expand Up @@ -9,6 +9,7 @@
import org.rapidoid.job.Jobs;
import org.rapidoid.log.Log;
import org.rapidoid.u.U;
import org.rapidoid.util.Once;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
Expand Down Expand Up @@ -59,8 +60,11 @@ public class Metrics extends RapidoidThing implements Runnable {

private static volatile ThreadMXBean threads;

private static final Once once = new Once();

public static void bootstrap() {
if (!once.go()) return;

Log.info("Bootstraping metrics");

os = ManagementFactory.getOperatingSystemMXBean();
Expand Down
47 changes: 47 additions & 0 deletions rapidoid-commons/src/main/java/org/rapidoid/util/Once.java
@@ -0,0 +1,47 @@
package org.rapidoid.util;

import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;

import java.util.concurrent.atomic.AtomicBoolean;

/*
* #%L
* rapidoid-commons
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors
* %%
* 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.
* #L%
*/

@Authors("Nikolche Mihajlovski")
@Since("5.1.0")
public class Once extends RapidoidThing {

private final AtomicBoolean done = new AtomicBoolean();

public boolean go() {
return !done.getAndSet(true);
}

public boolean isDone() {
return done.get();
}

public void reset() {
done.set(false);
}

}
1 change: 1 addition & 0 deletions rapidoid-commons/src/main/resources/rapidoid-classes.txt
Expand Up @@ -557,6 +557,7 @@ org.rapidoid.util.MapEntry
org.rapidoid.util.Msc
org.rapidoid.util.MscInfo
org.rapidoid.util.NullOutputStream
org.rapidoid.util.Once
org.rapidoid.util.RapidoidProcess
org.rapidoid.util.Resetable
org.rapidoid.util.Role
Expand Down
34 changes: 13 additions & 21 deletions rapidoid-http-server/src/main/java/org/rapidoid/setup/Setup.java
Expand Up @@ -38,6 +38,7 @@
import org.rapidoid.util.Constants;
import org.rapidoid.util.Msc;
import org.rapidoid.util.MscInfo;
import org.rapidoid.util.Once;

import java.lang.annotation.Annotation;
import java.util.List;
Expand Down Expand Up @@ -124,9 +125,9 @@ public void run() {
private volatile boolean activated;
private volatile boolean goodies = true;

private volatile boolean bootstrapedComponents;
private volatile boolean bootstrapedJPA;
private volatile boolean bootstrapedGoodies;
private final Once bootstrapedComponents = new Once();
private final Once bootstrapedJPA = new Once();
private final Once bootstrapedGoodies = new Once();

public static Setup create(String name) {
IoCContext ioc = IoC.createContext().name(name);
Expand Down Expand Up @@ -374,9 +375,9 @@ public void reset() {
MscInfo.isAdminActive = false;
}

bootstrapedJPA = false;
bootstrapedComponents = false;
bootstrapedGoodies = false;
bootstrapedJPA.reset();
bootstrapedComponents.reset();
bootstrapedGoodies.reset();
}

public Server server() {
Expand Down Expand Up @@ -452,10 +453,7 @@ private void setupConfig() {
}

public Setup bootstrapJPA() {
if (bootstrapedJPA) {
return this;
}
bootstrapedJPA = true;
if (!bootstrapedJPA.go()) return this;

if (Msc.hasJPA()) {
JPA.bootstrap(path());
Expand All @@ -466,10 +464,7 @@ public Setup bootstrapJPA() {

@SuppressWarnings("unchecked")
public Setup bootstrapComponents() {
if (bootstrapedComponents) {
return this;
}
bootstrapedComponents = true;
if (!bootstrapedComponents.go()) return this;

List<Class<? extends Annotation>> annotated = U.list(Controller.class, Service.class, Main.class);

Expand All @@ -483,10 +478,7 @@ public Setup bootstrapComponents() {
}

public Setup bootstrapGoodies() {
if (bootstrapedGoodies) {
return this;
}
bootstrapedGoodies = true;
if (!bootstrapedGoodies.go()) return this;

Class<?> goodiesClass = Cls.getClassIfExists("org.rapidoid.goodies.RapidoidGoodiesModule");

Expand Down Expand Up @@ -613,9 +605,9 @@ public RouteOptions defaults() {
}

public void resetWithoutRestart() {
bootstrapedJPA = false;
bootstrapedComponents = false;
bootstrapedGoodies = false;
bootstrapedJPA.reset();
bootstrapedComponents.reset();
bootstrapedGoodies.reset();

ioCContext.reset();
http().resetConfig();
Expand Down
Expand Up @@ -6,6 +6,7 @@
import org.rapidoid.setup.Admin;
import org.rapidoid.setup.On;
import org.rapidoid.util.Msc;
import org.rapidoid.util.Once;

/*
* #%L
Expand All @@ -31,20 +32,18 @@
@Since("4.0.0")
public class Rapidoid extends RapidoidThing {

private static boolean initialized;
private static final Once once = new Once();

private Rapidoid() {
}

public static synchronized void run(String... args) {
if (!initialized) {
initialized = true;
if (!once.go()) return;

On.bootstrap(args);
Admin.bootstrap(args);
On.bootstrap(args);
Admin.bootstrap(args);

Msc.logSection("Rapidoid bootstrap completed");
}
Msc.logSection("Rapidoid bootstrap completed");
}

}

0 comments on commit 94c6919

Please sign in to comment.