forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): optimize api service startup
- Loading branch information
1 parent
e7c8d40
commit 98eba8e
Showing
25 changed files
with
175 additions
and
335 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
framework/src/main/java/org/tron/common/application/AbstractService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.tron.common.application; | ||
|
||
import com.google.common.base.Objects; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
|
||
@Slf4j(topic = "service") | ||
public abstract class AbstractService implements Service { | ||
|
||
protected int port; | ||
@Getter | ||
protected boolean enable; | ||
protected final String name = this.getClass().getSimpleName(); | ||
protected final AtomicBoolean started = new AtomicBoolean(false); | ||
protected final AtomicBoolean stopped = new AtomicBoolean(false); | ||
|
||
|
||
@Override | ||
public void start() { | ||
if (started.compareAndSet(false, true)) { | ||
try { | ||
innerStart(); | ||
logger.info("{} started, listening on {}", name, port); | ||
} catch (Exception e) { | ||
logger.error("{}", name, e); | ||
} | ||
} else { | ||
logger.error("Attempted to start already running {}.", name); | ||
} | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
if (started.get() && stopped.compareAndSet(false, true)) { | ||
logger.info("{} shutdown...", name); | ||
try { | ||
innerStop(); | ||
} catch (Exception e) { | ||
logger.warn("{}", name, e); | ||
} | ||
logger.info("{} shutdown complete", name); | ||
} else { | ||
logger.error("Attempted to stop already stopped {}.", name); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
AbstractService that = (AbstractService) o; | ||
return port == that.port; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(name, port); | ||
} | ||
|
||
public abstract void innerStart() throws Exception; | ||
|
||
public abstract void innerStop() throws Exception; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.