forked from External/mage
Merge pull request #7158 from fburato/app-wiring-refactor
Application wiring refactor and externalise configuration path for server
This commit is contained in:
commit
81e0cc6403
59 changed files with 2367 additions and 779 deletions
|
|
@ -0,0 +1,20 @@
|
|||
package mage.server.util;
|
||||
|
||||
import mage.server.util.config.Config;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import java.io.File;
|
||||
|
||||
public class ConfigFactory {
|
||||
|
||||
public static Config loadFromFile(final String filePath) {
|
||||
try {
|
||||
final JAXBContext jaxbContext = JAXBContext.newInstance("mage.server.util.config");
|
||||
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
||||
return (Config) unmarshaller.unmarshal(new File(filePath));
|
||||
} catch (Exception e) {
|
||||
throw new ConfigurationException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +1,18 @@
|
|||
|
||||
package mage.server.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import mage.server.managers.ConfigSettings;
|
||||
import mage.server.util.config.Config;
|
||||
import mage.server.util.config.GamePlugin;
|
||||
import mage.server.util.config.Plugin;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
/**
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public enum ConfigSettings {
|
||||
instance;
|
||||
private final Logger logger = Logger.getLogger(ConfigSettings.class);
|
||||
import java.util.List;
|
||||
|
||||
private Config config;
|
||||
public class ConfigWrapper implements ConfigSettings {
|
||||
|
||||
ConfigSettings() {
|
||||
try {
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance("mage.server.util.config");
|
||||
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
||||
config = (Config) unmarshaller.unmarshal(new File("config/config.xml"));
|
||||
} catch (JAXBException ex) {
|
||||
logger.fatal("ConfigSettings error", ex);
|
||||
}
|
||||
private final Config config;
|
||||
|
||||
public ConfigWrapper(final Config config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public String getServerAddress() {
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package mage.server.util;
|
||||
|
||||
public class ConfigurationException extends RuntimeException {
|
||||
public ConfigurationException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,24 +1,18 @@
|
|||
|
||||
package mage.server.util;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import mage.server.managers.ConfigSettings;
|
||||
import mage.server.managers.ThreadExecutor;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public enum ThreadExecutor {
|
||||
instance;
|
||||
private static final ExecutorService callExecutor = Executors.newCachedThreadPool();
|
||||
private static final ExecutorService userExecutor = Executors.newCachedThreadPool();
|
||||
private static final ExecutorService gameExecutor = Executors.newFixedThreadPool(ConfigSettings.instance.getMaxGameThreads());
|
||||
private static final ScheduledExecutorService timeoutExecutor = Executors.newScheduledThreadPool(4);
|
||||
private static final ScheduledExecutorService timeoutIdleExecutor = Executors.newScheduledThreadPool(4);
|
||||
public class ThreadExecutorImpl implements ThreadExecutor {
|
||||
private final ExecutorService callExecutor;
|
||||
private final ExecutorService gameExecutor;
|
||||
private final ScheduledExecutorService timeoutExecutor;
|
||||
private final ScheduledExecutorService timeoutIdleExecutor;
|
||||
|
||||
/**
|
||||
* noxx: what the settings below do is setting the ability to keep OS
|
||||
|
|
@ -26,17 +20,20 @@ instance;
|
|||
* within this time period, the thread may be discarded. But anyway if new
|
||||
* game is created later, new OS/java thread will be created for it taking
|
||||
* MaxGameThreads limit into account.
|
||||
*
|
||||
* <p>
|
||||
* This all is done for performance reasons as creating new OS threads is
|
||||
* resource consuming process.
|
||||
*/
|
||||
static {
|
||||
|
||||
public ThreadExecutorImpl(ConfigSettings config) {
|
||||
callExecutor = Executors.newCachedThreadPool();
|
||||
gameExecutor = Executors.newFixedThreadPool(config.getMaxGameThreads());
|
||||
timeoutExecutor = Executors.newScheduledThreadPool(4);
|
||||
timeoutIdleExecutor = Executors.newScheduledThreadPool(4);
|
||||
|
||||
((ThreadPoolExecutor) callExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
|
||||
((ThreadPoolExecutor) callExecutor).allowCoreThreadTimeOut(true);
|
||||
((ThreadPoolExecutor) callExecutor).setThreadFactory(new XMageThreadFactory("CALL"));
|
||||
((ThreadPoolExecutor) userExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
|
||||
((ThreadPoolExecutor) userExecutor).allowCoreThreadTimeOut(true);
|
||||
((ThreadPoolExecutor) userExecutor).setThreadFactory(new XMageThreadFactory("USER"));
|
||||
((ThreadPoolExecutor) gameExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
|
||||
((ThreadPoolExecutor) gameExecutor).allowCoreThreadTimeOut(true);
|
||||
((ThreadPoolExecutor) gameExecutor).setThreadFactory(new XMageThreadFactory("GAME"));
|
||||
|
|
@ -49,6 +46,7 @@ instance;
|
|||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getActiveThreads(ExecutorService executerService) {
|
||||
if (executerService instanceof ThreadPoolExecutor) {
|
||||
return ((ThreadPoolExecutor) executerService).getActiveCount();
|
||||
|
|
@ -56,18 +54,22 @@ instance;
|
|||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExecutorService getCallExecutor() {
|
||||
return callExecutor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExecutorService getGameExecutor() {
|
||||
return gameExecutor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledExecutorService getTimeoutExecutor() {
|
||||
return timeoutExecutor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledExecutorService getTimeoutIdleExecutor() {
|
||||
return timeoutIdleExecutor;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue