Last active
December 11, 2015 03:29
-
-
Save write2munish/4538194 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * Licensed to the Apache Software Foundation (ASF) under one or more | |
| * contributor license agreements. See the NOTICE file distributed with | |
| * this work for additional information regarding copyright ownership. | |
| * The ASF licenses this file to You 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. | |
| */ | |
| package org.apache.geronimo.samples.daytrader.core; | |
| import java.math.BigDecimal; | |
| import java.util.ArrayList; | |
| import java.util.Random; | |
| import org.apache.geronimo.samples.daytrader.beans.AccountDataBean; | |
| import org.apache.geronimo.samples.daytrader.beans.AccountProfileDataBean; | |
| import org.apache.geronimo.samples.daytrader.beans.HoldingDataBean; | |
| import org.apache.geronimo.samples.daytrader.beans.OrderDataBean; | |
| import org.apache.geronimo.samples.daytrader.beans.QuoteDataBean; | |
| import org.apache.geronimo.samples.daytrader.core.direct.TradeJDBCDirect; | |
| import org.apache.geronimo.samples.daytrader.core.direct.TradeJEEDirect; | |
| import org.apache.geronimo.samples.daytrader.core.direct.TradeJPADirect; | |
| import org.apache.geronimo.samples.daytrader.core.MarketSummaryDataBean; | |
| import org.apache.geronimo.samples.daytrader.core.RunStatsDataBean; | |
| import org.apache.geronimo.samples.daytrader.core.*; | |
| import org.apache.geronimo.samples.daytrader.util.*; | |
| import akka.actor.*; | |
| import akka.actor.ActorRef; | |
| import akka.actor.ActorSystem; | |
| import akka.actor.Props; | |
| import akka.actor.UntypedActor; | |
| import akka.actor.UntypedActorFactory; | |
| import akka.routing.RoundRobinRouter; | |
| import akka.util.Duration; | |
| import java.util.concurrent.TimeUnit; | |
| import akka.util.Timeout; | |
| import com.typesafe.config.*; | |
| import akka.actor.TypedActor; | |
| import akka.dispatch.*; | |
| import akka.japi.*; | |
| /** | |
| * TradeConfig is a JavaBean holding all configuration and runtime parameters for the Trade application | |
| * TradeConfig sets runtime parameters such as the RunTimeMode (EJB3, DIRECT, SESSION3, JDBC, JPA) | |
| * | |
| */ | |
| public class TradeActionManager { | |
| public static int MAX_TRADE_ACTION_ACTORS = 10; | |
| //private static ActorRef[] TradeActionActorRefList; | |
| private static TradeServices[] TradeActionList; | |
| //private static ActorRef TradeActionRouter; | |
| private static Object TradeAction_count_semaphore = new Object(); | |
| private static Object TradeActionManager_semaphore = new Object(); | |
| private static int TradeAction_count = 0; | |
| public static int isInitialized =0; | |
| private static String conf = " my-thread-pool-dispatcher {\n# Dispatcher is the name of the event-based dispatcher\ntype = BalancingDispatcher\n# What kind of ExecutionService to use\nexecutor = \"thread-pool-executor\"\n# Configuration for the thread pool\nthread-pool-executor {\n# minimum number of threads to cap factor-based core number to\ncore-pool-size-min = 2\n# No of core threads ... ceil(available processors * factor)\ncore-pool-size-factor = 2.0\n" + "# maximum number of threads to cap factor-based number to\ncore-pool-size-max = 10\n}\n# Throughput defines the maximum number of messages to be\n# processed per actor before the thread jumps to the next actor.\n# Set to 1 for as fair as possible.\nthroughput = 100\n}\n"; | |
| private static Config customConf = ConfigFactory.parseString(conf); | |
| private static ActorSystem system; | |
| public static void getTradeActionActors() { | |
| synchronized (TradeAction_count_semaphore) { | |
| if(isInitialized == 1) | |
| { | |
| return; | |
| } | |
| try { | |
| TradeAction tradeaction; | |
| MAX_TRADE_ACTION_ACTORS = TradeConfig.getMAX_QUOTES_WORKERS(); | |
| system = ActorSystem.create(String.format("TradeActionActorSystem"),ConfigFactory.load(customConf)); | |
| isInitialized = 2; | |
| TradeActionList = new TradeServices[MAX_TRADE_ACTION_ACTORS]; | |
| for (int i = 0; i < MAX_TRADE_ACTION_ACTORS; i++) { | |
| TradeActionList[i] = | |
| TypedActor.get(system).typedActorOf((new TypedProps<TradeAction>(TradeServices.class, TradeAction.class).withDispatcher("my-thread-pool-dispatcher") ).withTimeout(new Timeout(100000))); | |
| } | |
| isInitialized =1; | |
| System.out.println(String.format("\n\tTRADE ACTION MANAGER CREATION - END")); | |
| } catch(Exception e) { | |
| System.out.println(e.toString()); | |
| } | |
| } | |
| } | |
| public static TradeServices getTradeAction() { | |
| TradeAction tradeaction; | |
| TradeServices ts; | |
| synchronized (TradeAction_count_semaphore) { | |
| ts = (TradeActionList[TradeAction_count]); | |
| TradeAction_count++; | |
| if (TradeAction_count % MAX_TRADE_ACTION_ACTORS == 0) { | |
| TradeAction_count = 0; | |
| } | |
| } | |
| return ts; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment