Skip to content

Commit

Permalink
Also use system property #2298
Browse files Browse the repository at this point in the history
Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed Mar 7, 2018
1 parent cf0b614 commit 35be19b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Expand Up @@ -19,21 +19,24 @@
package org.eclipse.jetty.util;

/**
* ProcessorUtils return the default value for processor number from {@link Runtime}
* but in a virtual environment you can override it using env var <code>JETTY_AVAILABLE_PROCESSORS</code>
* ProcessorUtils provides access to runtime info about processors, that may be
* overridden by system properties of environment variables. This can be useful
* in virtualised environments where the runtime may miss report the available
* resources.
*/
public class ProcessorUtils
{
private static int AVAILABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
public static final String AVAILABLE_PROCESSORS = "JETTY_AVAILABLE_PROCESSORS";
private static int __availableProcessors = Runtime.getRuntime().availableProcessors();

static
{
String avlProcEnv = System.getenv( "JETTY_AVAILABLE_PROCESSORS" );
String avlProcEnv = System.getProperty(AVAILABLE_PROCESSORS,System.getenv(AVAILABLE_PROCESSORS));
if (avlProcEnv != null)
{
try
{
AVAILABLE_PROCESSORS = Integer.parseInt( avlProcEnv );
__availableProcessors = Integer.parseInt( avlProcEnv );
}
catch ( NumberFormatException e )
{
Expand All @@ -43,11 +46,13 @@ public class ProcessorUtils
}

/**
*
* Obtain the number of available processors, from System Property "JETTY_AVAILABLE_PROCESSORS",
* or if not set then environment variable "JETTY_AVAILABLE_PROCESSORS" or if not set then
* {@link Runtime#availableProcessors()}.
* @return the number of processors
*/
public static int availableProcessors()
{
return AVAILABLE_PROCESSORS;
return __availableProcessors;
}
}
Expand Up @@ -19,18 +19,23 @@
package org.eclipse.jetty.util;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* we cannot really add env var in a unit test... so only test we get default value
*/
public class ProcessorUtilsTest
{

@BeforeClass
public static void beforeClass()
{
System.setProperty("JETTY_AVAILABLE_PROCESSORS","42");
}

@Test
public void get_default_value(){
Assert.assertEquals(Runtime.getRuntime().availableProcessors(), ProcessorUtils.availableProcessors());
public void getPropertyValue()
{
Assert.assertEquals(42, ProcessorUtils.availableProcessors());
}


}

0 comments on commit 35be19b

Please sign in to comment.