List of usage examples for java.util.concurrent ArrayBlockingQueue ArrayBlockingQueue
public ArrayBlockingQueue(int capacity)
From source file:ubic.gemma.loader.protein.StringProteinInteractionLoader.java
/** * Method to generate and load Gene2GeneProteinAssociation one taxon at a time * //w ww .ja v a2 s . c o m * @param ensembl2ncbi Map of peptide ids to NCBI gene ids * @param proteinInteractionsOneTaxon The protein interactions representing one taxon */ public void loadOneTaxonAtATime(Map<String, Ensembl2NcbiValueObject> ensembl2ncbi, Collection<StringProteinProteinInteraction> proteinInteractionsOneTaxon) { long startTime = System.currentTimeMillis(); converterDone.set(false); loaderDone.set(false); loadedGeneCount = 0; // generate gemma objects StringProteinProteinInteractionConverter converter = new StringProteinProteinInteractionConverter( ensembl2ncbi); converter.setStringExternalDatabase(this.getExternalDatabaseForString()); // create queue for String objects to be converted final BlockingQueue<Gene2GeneProteinAssociation> gene2GeneProteinAssociationQueue = new ArrayBlockingQueue<Gene2GeneProteinAssociation>( QUEUE_SIZE); converter.setProducerDoneFlag(converterDone); converter.convert(gene2GeneProteinAssociationQueue, proteinInteractionsOneTaxon); // Threaded consumer. Consumes Gene objects and persists them into the database this.load(gene2GeneProteinAssociationQueue); log.debug("Time taken to load data in minutes is " + (((System.currentTimeMillis() / 1000) - (startTime) / 1000)) / 60); }
From source file:org.diorite.config.serialization.snakeyaml.emitter.Emitter.java
public Emitter(Serialization serialization, Writer stream, DumperOptions opts) { this.serialization = serialization; // The stream should have the methods `write` and possibly `flush`. this.stream = stream; // Emitter is a state machine with a stack of states to handle nested // structures. this.states = new ArrayStack<>(100); this.state = new ExpectStreamStart(); // Current event and the event queue. this.events = new ArrayBlockingQueue<>(100); this.event = null; // The current indentation level and the stack of previous indents. this.indents = new ArrayStack<>(10); this.indent = null; // Flow level. this.flowLevel = 0; // Contexts.//from w w w.j a va 2 s . c om this.mappingContext = false; this.simpleKeyContext = false; // // Characteristics of the last emitted character: // - current position. // - is it a whitespace? // - is it an indention character // (indentation space, '-', '?', or ':')? this.column = 0; this.whitespace = true; this.indention = true; // Whether the document requires an explicit document indicator this.openEnded = false; // Formatting details. this.canonical = opts.isCanonical(); this.prettyFlow = opts.isPrettyFlow(); this.allowUnicode = opts.isAllowUnicode(); this.bestIndent = 2; if ((opts.getIndent() > MIN_INDENT) && (opts.getIndent() < MAX_INDENT)) { this.bestIndent = opts.getIndent(); } this.indicatorIndent = opts.getIndicatorIndent(); this.bestWidth = BEST_WIDTH; if (opts.getWidth() > (this.bestIndent * 2)) { this.bestWidth = opts.getWidth(); } this.bestLineBreak = opts.getLineBreak().getString().toCharArray(); this.splitLines = opts.getSplitLines(); this.longCommentThreshold = opts.getLongCommentThreshold(); this.longCommentBorder = opts.getLongCommentBorder(); this.longCommentBorderStartsWithComment = !this.longCommentBorder.isEmpty() && (this.longCommentBorder.charAt(0) == '#'); this.longCommentRightBorder = opts.getLongCommentRightBorder(); // Tag prefixes. this.tagPrefixes = new LinkedHashMap<>(10); // Prepared anchor and tag. this.preparedAnchor = null; this.preparedTag = null; // Scalar analysis and style. this.analysis = null; this.style = null; }
From source file:dk.dma.ais.lib.FileConvert.java
/** {@inheritDoc} */ @Override//from w w w .j a va2 s . c o m protected void run(Injector injector) throws Exception { configureFileEnding(); final EConsumer<String> consumer = new EConsumer<String>() { @Override public void accept(String s) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, IOException, InterruptedException { Path path = Paths.get(s); LOG.debug("Started processing file " + path); Path endPath; if (keepFileStructure) { Path relative; relative = path; endPath = Paths.get(Paths.get(convertTo).toString(), relative.toString()); new File(endPath.toString()).mkdirs(); } else { endPath = Paths.get(""); } String filename = path.getFileName().toString(); if (!filename.endsWith(fileEnding)) filename = FilenameUtils.removeExtension(filename) + fileEnding; Path filePath = Paths.get(endPath.toString(), filename); LOG.debug("Output File: " + filePath.toString()); final OutputStream fos = new FileOutputStream(filePath.toString()); // 2 final boolean createSituationFolder = !StringUtils.isBlank(kmzSnapshotAt); final long snapshotAtEpochMillis = createSituationFolder ? LocalDateTime.parse(kmzSnapshotAt, formatter).toInstant(ZoneOffset.UTC).toEpochMilli() : -1; OutputStreamSink<AisPacket> sink; if ("kmz".equals(outputSinkFormat)) { //AisPacketKMZOutputSink(filter, createSituationFolder, createMovementsFolder, createTracksFolder, isPrimaryTarget, isSecondaryTarget, triggerSnapshot, snapshotDescriptionSupplier, movementInterpolationStep, supplyTitle, supplyDescription, iconHrefSupplier); sink = AisPacketOutputSinks.newKmzSink(e -> true, // this.filter = e -> true; createSituationFolder, // this.createSituationFolder = true; true, // createMovementsFolder = true; true, // this.createTracksFolder = true; e -> kmzPrimaryMmsi <= 0 ? false : e.tryGetAisMessage().getUserId() == kmzPrimaryMmsi, // this.isPrimaryTarget = e -> false; e -> kmzSecondaryMmsi <= 0 ? false : e.tryGetAisMessage().getUserId() == kmzSecondaryMmsi, // this.isSecondaryTarget = e -> false; e -> e.getBestTimestamp() >= snapshotAtEpochMillis, // this.triggerSnapshot = e -> false; () -> "Situation at " + kmzSnapshotAt, // this.snapshotDescriptionSupplier = null; () -> 10, // this.title = defaultTitleSupplier; () -> "description", // this.description = defaultDescriptionSupplier; () -> "10", //this.movementInterpolationStep = defaultMovementInterpolationStepSupplier; (shipTypeCargo, navigationalStatus) -> "" // this.iconHrefSupplier = defaultIconHrefSupplier; ); } else sink = AisPacketOutputSinks.getOutputSink(outputSinkFormat, columns); sink.closeWhenFooterWritten(); AisPacketReader apis = AisPacketReader.createFromFile(path, false); apis.writeTo(fos, sink); apis.close(); fos.close(); } }; /* * Creates a pool of executors, 4 threads. Each thread will open a file using an aispacket reader, 10000 files can be * submitted to the queue, afterwards the calling thread will execute the job instead. */ ThreadPoolExecutor threadpoolexecutor = new ThreadPoolExecutor(4, 4, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10000), new ThreadPoolExecutor.CallerRunsPolicy()); for (final String s : sources) { threadpoolexecutor.execute(() -> { try { consumer.accept(s); } catch (Exception e) { e.printStackTrace(); } }); } threadpoolexecutor.shutdown(); threadpoolexecutor.awaitTermination(999, TimeUnit.DAYS); }
From source file:org.lilyproject.testclientfw.BaseTestTool.java
public void startExecutor() { executor = new ThreadPoolExecutor(workers, workers, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100)); executor.setRejectedExecutionHandler(new WaitPolicy()); }
From source file:com.uroad.net.AsyncHttpClient.java
public AsyncHttpClient() { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUserAgent(httpParams, String.format("uroad-android-httpclient/%s (http://www.u-road.com/)", VERSION)); //Scheme??"http""https"??? //??socket??java.net.SocketSchemeRegistry?Schemes? SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); //HTTPClient?? //?//from w w w . j av a2 s .c om httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } for (String header : clientHeaderMap.keySet()) { request.addHeader(header, clientHeaderMap.get(header)); } } }); //?? httpClient.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } }); httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES)); threadPool = new ThreadPoolExecutor(DEFAULT_CORE_POOL_SIZE, DEFAULT_MAXIMUM_POOL_SIZE, DEFAULT_KEEP_ALIVETIME, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3), new ThreadPoolExecutor.CallerRunsPolicy()); requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>(); clientHeaderMap = new HashMap<String, String>(); }
From source file:com.pinterest.rocksplicator.controller.DispatcherTest.java
@Test public void testChainedTaskWithError() throws Exception { TaskBase task = new SleepIncrementTask(100).andThen(new ThrowingTask("Oops...")) .andThen(new SleepIncrementTask(150)).getEntity(); final CountDownLatch latch = new CountDownLatch(2); FIFOTaskQueue tq = new FIFOTaskQueue(10) { @Override//from w w w . j a va 2 s. c o m public boolean finishTask(final long id, final String output) { latch.countDown(); return super.finishTask(id, output); } @Override public boolean failTask(final long id, final String reason) { latch.countDown(); return super.failTask(id, reason); } @Override public long finishTaskAndEnqueueRunningTask(final long id, final String output, final TaskBase newTask, final String worker) { latch.countDown(); return super.finishTaskAndEnqueueRunningTask(id, output, newTask, worker); } }; tq.enqueueTask(task, Integer.toString(++nameCounter), 0); Semaphore idleWorkersSemaphore = new Semaphore(2); ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2)); WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, tq); TaskDispatcher dispatcher = new TaskDispatcher(2, idleWorkersSemaphore, workerPool, tq); dispatcher.start(); Assert.assertTrue(latch.await(30, TimeUnit.SECONDS)); Assert.assertEquals(SleepIncrementTask.executionCounter.intValue(), 1); Assert.assertEquals(tq.getResult(0), "0"); Assert.assertEquals(tq.getResult(1), "Oops..."); dispatcher.stop(); }
From source file:org.apache.solr.client.solrj.impl.BackupRequestLBHttpSolrClient.java
/** * Tries to query a live server from the list provided in Req. Servers in the * dead pool are skipped. If a request fails due to an IOException, the server * is moved to the dead pool for a certain period of time, or until a test * request on that server succeeds./* w w w . j ava2 s . co m*/ * * If a request takes longer than defaultBackUpRequestDelay the request will be sent * to the next server in the list, this will continue until there is a * response, the server list is exhausted or the number of requests in flight * equals defaultMaximumConcurrentRequests. * * Servers are queried in the exact order given (except servers currently in * the dead pool are skipped). If no live servers from the provided list * remain to be tried, a number of previously skipped dead servers will be * tried. Req.getNumDeadServersToTry() controls how many dead servers will be * tried. * * If no live servers are found a SolrServerException is thrown. * * @param req * contains both the request as well as the list of servers to query * * @return the result of the request */ @Override public Rsp request(Req req) throws SolrServerException, IOException { SolrParams reqParams = req.getRequest().getParams(); int maximumConcurrentRequests = reqParams == null ? defaultMaximumConcurrentRequests : reqParams.getInt(HttpBackupRequestShardHandlerFactory.MAX_CONCURRENT_REQUESTS, defaultMaximumConcurrentRequests); // If we can't do anything useful, fall back to the stock solr code if (maximumConcurrentRequests < 0) { return super.request(req); } // if there's an explicit backupDelay in the request, use that int backupDelay = reqParams == null ? -1 : reqParams.getInt(HttpBackupRequestShardHandlerFactory.BACKUP_REQUEST_DELAY, -1); BackupPercentile backupPercentile = defaultBackupPercentile; String backupPercentileParam = reqParams == null ? null : reqParams.get(HttpBackupRequestShardHandlerFactory.BACKUP_PERCENTILE); if (backupPercentileParam != null) { backupPercentile = getPercentile(backupPercentileParam); } String performanceClass = reqParams == null ? req.getRequest().getPath() // getPath is typically the request handler name : reqParams.get(HttpBackupRequestShardHandlerFactory.PERFORMANCE_CLASS, reqParams.get(CommonParams.QT, req.getRequest().getPath())); // TODO: Is QT getting filtered out of the distrib requests? if (backupDelay < 0 && backupPercentile != BackupPercentile.NONE) { // no explicit backup delay, consider a backup percentile for the delay. double rate = getCachedRate(performanceClass); if (rate > 0.1) { // 1 request per 10 seconds minimum. backupDelay = getCachedPercentile(performanceClass, backupPercentile); log.debug("Using delay of {}ms for percentile {} for performanceClass {}", backupDelay, backupPercentile.name(), performanceClass); } else { log.info( "Insufficient query rate ({} per sec) to rely on latency percentiles for performanceClass {}", rate, performanceClass); } } else { // not using a percentile to track backupDelay performanceClass = null; } if (backupDelay < 0) { backupDelay = defaultBackUpRequestDelay; } // If we are using a backupPercentile, we need to proceed regardless of backupDelay so we can record and build the percentile info. // If not, and we still don't have a backupDelay, fall back to stock solr code. if (backupPercentile == BackupPercentile.NONE && backupDelay < 0) { return super.request(req); } // Reaching this point with a backupDelay < 0 means backup requests are effectively disabled, but we're executing // this codepath anyway. Presumably in order to build latency percentile data for future requests. ArrayBlockingQueue<Future<RequestTaskState>> queue = new ArrayBlockingQueue<Future<RequestTaskState>>( maximumConcurrentRequests + 1); ExecutorCompletionService<RequestTaskState> executer = new ExecutorCompletionService<RequestTaskState>( threadPoolExecuter, queue); final int numDeadServersToTry = req.getNumDeadServersToTry(); final boolean isUpdate = req.getRequest() instanceof IsUpdateRequest; List<ServerWrapper> skipped = null; int inFlight = 0; RequestTaskState returnedRsp = null; Exception ex = null; long timeAllowedNano = getTimeAllowedInNanos(req.getRequest()); long timeOutTime = System.nanoTime() + timeAllowedNano; for (String serverStr : req.getServers()) { if (isTimeExceeded(timeAllowedNano, timeOutTime)) { break; } serverStr = normalize(serverStr); // if the server is currently a zombie, just skip to the next one ServerWrapper wrapper = zombieServers.get(serverStr); if (wrapper != null) { if (tryDeadServers && numDeadServersToTry > 0) { if (skipped == null) { skipped = new ArrayList<>(numDeadServersToTry); skipped.add(wrapper); } else if (skipped.size() < numDeadServersToTry) { skipped.add(wrapper); } } continue; } HttpSolrClient client = makeSolrClient(serverStr); Callable<RequestTaskState> task = createRequestTask(client, req, isUpdate, false, null, performanceClass, inFlight > 0); executer.submit(task); inFlight++; returnedRsp = getResponseIfReady(executer, patience(inFlight, maximumConcurrentRequests, backupDelay)); if (returnedRsp == null) { // null response signifies that the response took too long. log.debug("Server :{} did not respond before the backupRequestDelay time of {} elapsed", client.baseUrl, backupDelay); continue; } inFlight--; if (returnedRsp.stateDescription == TaskState.ResponseReceived) { return returnedRsp.response; } else if (returnedRsp.stateDescription == TaskState.ServerException) { ex = returnedRsp.exception; } else if (returnedRsp.stateDescription == TaskState.RequestException) { throw new SolrServerException(returnedRsp.exception); } } // no response so try the zombie servers if (tryDeadServers && skipped != null) { if (returnedRsp == null || returnedRsp.stateDescription == TaskState.ServerException) { // try the servers we previously skipped for (ServerWrapper wrapper : skipped) { if (isTimeExceeded(timeAllowedNano, timeOutTime)) { break; } Callable<RequestTaskState> task = createRequestTask(wrapper.client, req, isUpdate, true, wrapper.getKey(), performanceClass, inFlight > 0); executer.submit(task); inFlight++; returnedRsp = getResponseIfReady(executer, patience(inFlight, maximumConcurrentRequests, backupDelay)); if (returnedRsp == null) { log.debug("Server :{} did not respond before the backupRequestDelay time of {} elapsed", wrapper.getKey(), backupDelay); continue; } inFlight--; if (returnedRsp.stateDescription == TaskState.ResponseReceived) { return returnedRsp.response; } else if (returnedRsp.stateDescription == TaskState.ServerException) { ex = returnedRsp.exception; } else if (returnedRsp.stateDescription == TaskState.RequestException) { throw new SolrServerException(returnedRsp.exception); } } } } // All current attempts could be slower than backUpRequestPause or returned // response could be from struggling server // so we need to wait until we get a good response or tasks all are // exhausted. if (returnedRsp == null || returnedRsp.stateDescription == TaskState.ServerException) { while (inFlight > 0) { returnedRsp = getResponseIfReady(executer, -1); inFlight--; if (returnedRsp.stateDescription == TaskState.ResponseReceived) { return returnedRsp.response; } else if (returnedRsp.stateDescription == TaskState.ServerException) { ex = returnedRsp.exception; } else if (returnedRsp.stateDescription == TaskState.RequestException) { throw new SolrServerException(returnedRsp.exception); } } } if (ex == null) { throw new SolrServerException("No live SolrServers available to handle this request"); } else { throw new SolrServerException( "No live SolrServers available to handle this request:" + zombieServers.keySet(), ex); } }
From source file:com.taobao.tdhs.client.statement.StatementImpl.java
protected TDHSResponse sendRequest(TDHSCommon.RequestType type, RequestWithCharset request, TDHSMetaData metaData) throws TDHSException { if (request == null) { throw new IllegalArgumentException("request can't be NULL!"); }//from w w w .j a va 2 s.c om if (StringUtils.isBlank(request.getCharsetName())) { //use default charsetName request.setCharsetName(this.charsetName); } byte data[] = version.getTdhsProtocol().encode(request); BasePacket packet = new BasePacket(type, id.getAndIncrement(), hash, data); ArrayBlockingQueue<BasePacket> queue = new ArrayBlockingQueue<BasePacket>(1); long seqId = packet.getSeqId(); responses.put(seqId, queue); try { tdhsNet.write(packet); return do_response(queue, metaData, request.getCharsetName()); } finally { responses.remove(seqId); queue = null; } }
From source file:ubic.gemma.apps.ArrayDesignBlatCli.java
@Override protected Exception doWork(String[] args) { Exception err = processCommandLine( "Array design sequence BLAT - only works if server is already started or if a PSL file is provided!", args);//from w w w. j a v a 2 s . c o m if (err != null) return err; final Date skipIfLastRunLaterThan = getLimitingDate(); if (!this.arrayDesignsToProcess.isEmpty()) { if (this.blatResultFile != null && this.arrayDesignsToProcess.size() > 1) { throw new IllegalArgumentException( "Cannot provide a blat result file when multiple arrays are being analyzed"); } for (ArrayDesign arrayDesign : this.arrayDesignsToProcess) { if (!needToRun(skipIfLastRunLaterThan, arrayDesign, ArrayDesignSequenceAnalysisEvent.class)) { log.warn(arrayDesign + " was last run more recently than " + skipIfLastRunLaterThan); return null; } arrayDesign = unlazifyArrayDesign(arrayDesign); Collection<BlatResult> persistedResults; try { if (this.blatResultFile != null) { Collection<BlatResult> blatResults = getBlatResultsFromFile(arrayDesign); if (blatResults == null || blatResults.size() == 0) { throw new IllegalStateException("No blat results in file!"); } log.info("Got " + blatResults.size() + " blat records"); persistedResults = arrayDesignSequenceAlignmentService.processArrayDesign(arrayDesign, taxon, blatResults); audit(arrayDesign, "BLAT results read from file: " + blatResultFile); } else { // Run blat from scratch. persistedResults = arrayDesignSequenceAlignmentService.processArrayDesign(arrayDesign, this.sensitive); audit(arrayDesign, "Based on a fresh alignment analysis; BLAT score threshold was " + this.blatScoreThreshold + "; sensitive mode was " + this.sensitive); } log.info("Persisted " + persistedResults.size() + " results"); } catch (FileNotFoundException e) { this.errorObjects.add(e); } catch (IOException e) { this.errorObjects.add(e); } } } else if (taxon != null) { Collection<ArrayDesign> allArrayDesigns = arrayDesignService.findByTaxon(taxon); log.warn("*** Running BLAT for all " + taxon.getCommonName() + " Array designs *** [" + allArrayDesigns.size() + " items]"); final SecurityContext context = SecurityContextHolder.getContext(); // split over multiple threads so we can multiplex. Put the array designs in a queue. /* * Here is our task runner. */ class Consumer implements Runnable { private final BlockingQueue<ArrayDesign> queue; public Consumer(BlockingQueue<ArrayDesign> q) { queue = q; } @Override public void run() { SecurityContextHolder.setContext(context); while (true) { ArrayDesign ad = queue.poll(); if (ad == null) { break; } consume(ad); } } void consume(ArrayDesign x) { x = arrayDesignService.thaw(x); processArrayDesign(skipIfLastRunLaterThan, x); } } BlockingQueue<ArrayDesign> arrayDesigns = new ArrayBlockingQueue<ArrayDesign>(allArrayDesigns.size()); for (ArrayDesign ad : allArrayDesigns) { arrayDesigns.add(ad); } /* * Start the threads */ Collection<Thread> threads = new ArrayList<Thread>(); for (int i = 0; i < this.numThreads; i++) { Consumer c1 = new Consumer(arrayDesigns); Thread k = new Thread(c1); threads.add(k); k.start(); } waitForThreadPoolCompletion(threads); /* * All done */ summarizeProcessing(); } else { bail(ErrorCode.MISSING_ARGUMENT); } return null; }
From source file:com.orion.bot.Orion.java
/** * Object constructor//from w w w . jav a 2 s. co m * * @author Daniele Pantaleone * @param path The Orion configuration file path **/ public Orion(String path) { try { // Loading the main XML configuration file this.config = new XmlConfiguration(path); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////// LOGGER SETUP ///////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Logger.getRootLogger().setLevel(Level.OFF); Logger logger = Logger.getLogger("Orion"); FileAppender fa = new FileAppender(); fa.setLayout(new PatternLayout("%-20d{yyyy-MM-dd hh:mm:ss} %-6p %m%n")); fa.setFile(this.config.getString("logfile", "basepath") + this.config.getString("logfile", "filename")); fa.setAppend(this.config.getBoolean("logfile", "append")); fa.setName("FILE"); fa.activateOptions(); logger.addAppender(fa); if (this.config.getBoolean("logfile", "console")) { ConsoleAppender ca = new ConsoleAppender(); ca.setLayout(new PatternLayout("%-20d{yyyy-MM-dd hh:mm:ss} %-6p %m%n")); ca.setWriter(new OutputStreamWriter(System.out)); ca.setName("CONSOLE"); ca.activateOptions(); logger.addAppender(ca); } // Setting the log level for both the log appenders logger.setLevel(Level.toLevel(this.config.getString("logfile", "level"))); // Creating the main Log object this.log = new Log4JLogger(logger); // We got a fully initialized logger utility now: printing some info messages this.log.info( "Starting " + BOTNAME + " " + VERSION + " [" + CODENAME + "] [ " + AUTHOR + " ] - " + WEBSITE); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////// LOADING PREFERENCES /////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// this.timeformat = this.config.getString("orion", "timeformat", "EEE, d MMM yyyy HH:mm:ss"); this.timezone = DateTimeZone.forID(this.config.getString("orion", "timezone", "CET")); this.locale = new Locale(this.config.getString("orion", "locale", "EN"), this.config.getString("orion", "locale", "EN")); this.startuptime = new DateTime(this.timezone); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////// PRE INITIALIZED OBJECTS //////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// this.eventBus = new EventBus("events"); this.schedule = new LinkedHashMap<String, Timer>(); this.game = new Game(); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////// STORAGE SETUP ///////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// this.storage = new MySqlDataSourceManager(this.config.getString("storage", "username"), this.config.getString("storage", "password"), this.config.getString("storage", "connection"), this.log); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////// BUFFERS SETUP //////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// this.commandqueue = new ArrayBlockingQueue<Command>(this.config.getInt("orion", "commandqueue", 100)); this.regcommands = new MultiKeyHashMap<String, String, RegisteredCommand>(); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////// CONSOLE SETUP //////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// this.console = (Console) Class .forName("com.orion.console." + this.config.getString("orion", "game") + "Console") .getConstructor(String.class, int.class, String.class, Orion.class) .newInstance(this.config.getString("server", "rconaddress"), this.config.getInt("server", "rconport"), this.config.getString("server", "rconpassword"), this); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////// CONTROLLERS SETUP ///////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// this.groups = new GroupC(this); this.clients = new ClientC(this); this.aliases = new AliasC(this); this.callvotes = new CallvoteC(this); this.ipaliases = new IpAliasC(this); this.penalties = new PenaltyC(this); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////// PARSER SETUP///////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// this.parser = (Parser) Class .forName("com.orion.parser." + this.config.getString("orion", "game") + "Parser") .getConstructor(Orion.class).newInstance(this); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////// LOADING PLUGINS ////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// this.plugins = new LinkedHashMap<String, Plugin>(); Map<String, String> pluginsList = this.config.getMap("plugins"); for (Map.Entry<String, String> entry : pluginsList.entrySet()) { try { this.log.debug("Loading plugin [ " + Character.toUpperCase(entry.getKey().charAt(0)) + entry.getKey().substring(1).toLowerCase() + " ]"); Plugin plugin = Plugin.getPlugin(entry.getKey(), new XmlConfiguration(entry.getValue(), this.log), this); this.plugins.put(entry.getKey(), plugin); } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ParserException e) { // Logging the Exception and keep processing other plugins. This will not stop Orion execution this.log.error("Unable to load plugin [ " + Character.toUpperCase(entry.getKey().charAt(0)) + entry.getKey().substring(1).toLowerCase() + " ]", e); } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////// PLUGINS SETUP ////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// for (Map.Entry<String, Plugin> entry : this.plugins.entrySet()) { Plugin plugin = entry.getValue(); plugin.onLoadConfig(); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////// PLUGINS STARTUP ///////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// for (Map.Entry<String, Plugin> entry : this.plugins.entrySet()) { Plugin plugin = entry.getValue(); // Check for the plugin to be enabled. onLoadConfig may have disabled // such plugin in case the plugin config file is non well formed if (plugin.isEnabled()) plugin.onStartup(); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////// GAME SERVER SYNC //////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// List<List<String>> status = this.console.getStatus(); if (status == null) { this.log.warn("Unable to synchronize current server status: RCON response is NULL"); return; } for (List<String> line : status) { // Dumping current user to build an infostring for the onClientConnect parser method Map<String, String> userinfo = this.console.dumpuser(Integer.parseInt(line.get(0))); // Not a valid client if (userinfo == null) continue; String infostring = new String(); for (Map.Entry<String, String> entry : userinfo.entrySet()) { // Appending <key|value> using infostring format infostring += "\\" + entry.getKey() + "\\" + entry.getValue(); } // Generating an EVT_CLIENT_CONNECT event for the connected client this.parser.parseLine("0:00 ClientUserinfo: " + line.get(0) + " " + infostring); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////// THREADS SETUP ///////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// this.reader = new Thread(new Reader(this.config.getString("server", "logfile"), this.config.getInt("server", "logdelay"), this)); this.commandproc = new Thread(new CommandProcessor(this)); this.reader.setName("READER"); this.commandproc.setName("COMMAND"); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////// THREADS STARTUP //////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// this.commandproc.start(); this.reader.start(); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////// NOTICE BOT RUNNING /////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// this.console.say( BOTNAME + " " + VERSION + " [" + CODENAME + "] - " + WEBSITE + " >> " + Color.GREEN + "ONLINE"); } catch (Exception e) { // Stopping Threads if they are alive if ((this.commandproc != null) && (this.commandproc.isAlive())) this.commandproc.interrupt(); if ((this.reader != null) && (this.reader.isAlive())) this.reader.interrupt(); // Logging the Exception. Orion is not going to work if an Exception is catched at startup time this.log.fatal("Unable to start " + BOTNAME + " " + VERSION + " [" + CODENAME + "] [ " + AUTHOR + " ] - " + WEBSITE, e); } }