From feea5566ba1db7c037bfd845854709a84fcdf3e9 Mon Sep 17 00:00:00 2001 From: David Stenglein Date: Mon, 15 Jul 2013 00:03:15 -0400 Subject: [PATCH 1/3] Removed the need for util class for thread naming. --- .../recipes/rss/netty/NettyServer.java | 12 +++-- .../rss/util/DescriptiveThreadFactory.java | 50 ------------------- 2 files changed, 8 insertions(+), 54 deletions(-) delete mode 100755 rss-core/src/main/java/com/netflix/recipes/rss/util/DescriptiveThreadFactory.java diff --git a/rss-core/src/main/java/com/netflix/recipes/rss/netty/NettyServer.java b/rss-core/src/main/java/com/netflix/recipes/rss/netty/NettyServer.java index 43f4afd..26de4a0 100755 --- a/rss-core/src/main/java/com/netflix/recipes/rss/netty/NettyServer.java +++ b/rss-core/src/main/java/com/netflix/recipes/rss/netty/NettyServer.java @@ -17,7 +17,8 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Maps; -import com.netflix.recipes.rss.util.DescriptiveThreadFactory; +import com.google.common.util.concurrent.ThreadFactoryBuilder; + import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.*; import org.jboss.netty.channel.group.ChannelGroup; @@ -143,12 +144,14 @@ public NettyServer build() { ThreadPoolExecutor bossPool = new ThreadPoolExecutor( numBossThreads, numBossThreads, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), - new DescriptiveThreadFactory("Boss-Thread")); + new ThreadFactoryBuilder().setNameFormat("Boss-Thread-%d") + .setDaemon(false).setPriority(Thread.NORM_PRIORITY).build()); ThreadPoolExecutor workerPool = new ThreadPoolExecutor( numWorkerThreads, numWorkerThreads, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), - new DescriptiveThreadFactory("Worker-Thread")); + new ThreadFactoryBuilder().setNameFormat("Worker-Thread-%d") + .setDaemon(false).setPriority(Thread.NORM_PRIORITY).build()); ChannelFactory nioServer = new NioServerSocketChannelFactory( bossPool, workerPool, numWorkerThreads); @@ -191,7 +194,8 @@ public PipelineFactory(Map handlers, ThreadPoolExecutor executorThreadPool = new ThreadPoolExecutor( NettyServer.cpus, NettyServer.cpus * 4, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), - new DescriptiveThreadFactory("Executor-Thread")); + new ThreadFactoryBuilder().setNameFormat("Executor-Thread-%d") + .setDaemon(false).setPriority(Thread.NORM_PRIORITY).build()); this.executionHandler = new ExecutionHandler(executorThreadPool); } else { diff --git a/rss-core/src/main/java/com/netflix/recipes/rss/util/DescriptiveThreadFactory.java b/rss-core/src/main/java/com/netflix/recipes/rss/util/DescriptiveThreadFactory.java deleted file mode 100755 index 2bad022..0000000 --- a/rss-core/src/main/java/com/netflix/recipes/rss/util/DescriptiveThreadFactory.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2012 Netflix, Inc. - * - * 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. - */ -package com.netflix.recipes.rss.util; - -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * Adds descriptive thread names for debugging purposes. Allows priority and daemon to be set, as well. - * - * @author Chris Fregly (chris@fregly.com) - */ -public class DescriptiveThreadFactory implements ThreadFactory { - private final String description; - private final int priority; - private final boolean daemon; - private final AtomicInteger n = new AtomicInteger(1); - - public DescriptiveThreadFactory(String description) { - this(description, Thread.NORM_PRIORITY, false); - } - - public DescriptiveThreadFactory(String description, int priority, - boolean daemon) { - this.description = description; - this.priority = priority; - this.daemon = daemon; - } - - public Thread newThread(Runnable runnable) { - String threadDescription = description + "-" + n.getAndIncrement(); - Thread thread = new Thread(runnable, threadDescription); - thread.setPriority(priority); - thread.setDaemon(daemon); - return thread; - } -} From 14a5756973ae524c531eca8d4f3422dc151ab11d Mon Sep 17 00:00:00 2001 From: David Stenglein Date: Mon, 15 Jul 2013 00:36:12 -0400 Subject: [PATCH 2/3] Factored RSSConstants out of BaseJettyServer, make it a little more friendly to re-use. --- .../recipes/rss/server/BaseJettyServer.java | 22 ++++++++++++++----- .../recipes/rss/server/EdgeServer.java | 19 +++++++++++----- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/rss-core/src/main/java/com/netflix/recipes/rss/server/BaseJettyServer.java b/rss-core/src/main/java/com/netflix/recipes/rss/server/BaseJettyServer.java index ff2a7bd..8fa5e57 100755 --- a/rss-core/src/main/java/com/netflix/recipes/rss/server/BaseJettyServer.java +++ b/rss-core/src/main/java/com/netflix/recipes/rss/server/BaseJettyServer.java @@ -29,7 +29,6 @@ import com.netflix.config.DynamicPropertyFactory; import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; import com.netflix.karyon.server.KaryonServer; -import com.netflix.recipes.rss.RSSConstants; /** * Base Jetty Server @@ -48,8 +47,19 @@ public class BaseJettyServer implements Closeable { protected final Injector injector; - public BaseJettyServer() { - System.setProperty(DynamicPropertyFactory.ENABLE_JMX, "true"); + private final String portPropertyName; + + private String webAppsDir; + + private String hystrixStreamPath; + + public BaseJettyServer(String portPropertyName, String webAppsDir, String hystrixStreamPath) { + + System.setProperty(DynamicPropertyFactory.ENABLE_JMX, "true"); + + this.portPropertyName = portPropertyName; + this.webAppsDir = webAppsDir; + this.hystrixStreamPath = hystrixStreamPath; this.karyonServer = new KaryonServer(); this.injector = karyonServer.initialize(); @@ -58,15 +68,15 @@ public BaseJettyServer() { public void start() { - final int port = ConfigurationManager.getConfigInstance().getInt(RSSConstants.JETTY_HTTP_PORT, Integer.MIN_VALUE); + final int port = ConfigurationManager.getConfigInstance().getInt(portPropertyName, Integer.MIN_VALUE); final Context context = new Context(jettyServer, "/", Context.SESSIONS); - context.setResourceBase(RSSConstants.WEBAPPS_DIR); + context.setResourceBase(webAppsDir); context.setClassLoader(Thread.currentThread().getContextClassLoader()); context.addServlet(JspServlet.class, "*.jsp"); // Enable hystrix.stream - context.addServlet(HystrixMetricsStreamServlet.class, RSSConstants.HYSTRIX_STREAM_PATH); + context.addServlet(HystrixMetricsStreamServlet.class, hystrixStreamPath); final Server server = new Server(port); server.setHandler(context); diff --git a/rss-edge/src/main/java/com/netflix/recipes/rss/server/EdgeServer.java b/rss-edge/src/main/java/com/netflix/recipes/rss/server/EdgeServer.java index 8471b51..338b9f6 100755 --- a/rss-edge/src/main/java/com/netflix/recipes/rss/server/EdgeServer.java +++ b/rss-edge/src/main/java/com/netflix/recipes/rss/server/EdgeServer.java @@ -20,6 +20,7 @@ import com.netflix.config.ConfigurationManager; import com.netflix.karyon.spi.PropertyNames; +import com.netflix.recipes.rss.RSSConstants; /** * Edge Server @@ -31,22 +32,28 @@ public class EdgeServer extends BaseJettyServer { .getLogger(EdgeServer.class); public EdgeServer() { + super(RSSConstants.JETTY_HTTP_PORT, RSSConstants.WEBAPPS_DIR, + RSSConstants.HYSTRIX_STREAM_PATH); } - + public static void main(final String[] args) throws Exception { System.setProperty("archaius.deployment.applicationId", "edge"); - System.setProperty(PropertyNames.SERVER_BOOTSTRAP_BASE_PACKAGES_OVERRIDE, "com.netflix"); + System.setProperty( + PropertyNames.SERVER_BOOTSTRAP_BASE_PACKAGES_OVERRIDE, + "com.netflix"); + + final String appId = ConfigurationManager.getDeploymentContext() + .getApplicationId(); + final String env = ConfigurationManager.getDeploymentContext() + .getDeploymentEnvironment(); - String appId = ConfigurationManager.getDeploymentContext().getApplicationId(); - String env = ConfigurationManager.getDeploymentContext().getDeploymentEnvironment(); - // populate the eureka-specific properties System.setProperty("eureka.client.props", appId); if (env != null) { System.setProperty("eureka.environment", env); } - EdgeServer edgeServer = new EdgeServer(); + final EdgeServer edgeServer = new EdgeServer(); edgeServer.start(); } } From 60a33a35170e95655b7dc6a05de0546511c474a6 Mon Sep 17 00:00:00 2001 From: David Stenglein Date: Mon, 15 Jul 2013 00:49:16 -0400 Subject: [PATCH 3/3] Restoring formatting wiped with save actions. --- .../com/netflix/recipes/rss/server/EdgeServer.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/rss-edge/src/main/java/com/netflix/recipes/rss/server/EdgeServer.java b/rss-edge/src/main/java/com/netflix/recipes/rss/server/EdgeServer.java index 338b9f6..0d4f4b3 100755 --- a/rss-edge/src/main/java/com/netflix/recipes/rss/server/EdgeServer.java +++ b/rss-edge/src/main/java/com/netflix/recipes/rss/server/EdgeServer.java @@ -38,22 +38,18 @@ public EdgeServer() { public static void main(final String[] args) throws Exception { System.setProperty("archaius.deployment.applicationId", "edge"); - System.setProperty( - PropertyNames.SERVER_BOOTSTRAP_BASE_PACKAGES_OVERRIDE, - "com.netflix"); - - final String appId = ConfigurationManager.getDeploymentContext() - .getApplicationId(); - final String env = ConfigurationManager.getDeploymentContext() - .getDeploymentEnvironment(); + System.setProperty(PropertyNames.SERVER_BOOTSTRAP_BASE_PACKAGES_OVERRIDE, "com.netflix"); + String appId = ConfigurationManager.getDeploymentContext().getApplicationId(); + String env = ConfigurationManager.getDeploymentContext().getDeploymentEnvironment(); + // populate the eureka-specific properties System.setProperty("eureka.client.props", appId); if (env != null) { System.setProperty("eureka.environment", env); } - final EdgeServer edgeServer = new EdgeServer(); + EdgeServer edgeServer = new EdgeServer(); edgeServer.start(); } }