Giter VIP home page Giter VIP logo

porcupine's Introduction

porcupine

Configurable threading, resiliency and monitoring with injectable statistics for Java EE 7. Porcupine is the implementation of the bulkhead and handshaking patterns for Java EE 7.

Features:

  1. Conventional: ExecutorService is directly injectable. The thread pool derives the name from the field, but can be easily overridden.
  2. Drop-in install: a single dependency in the pom.xml is sufficient.
  3. Standard based: porcupine uses JSR 236: Concurrency Utilities for JavaTM EE
  4. Small: the entire framework is: 16kB.
  5. Extensible without configuration: All major components can be replaced (big thanks to @Specializes)

Booting Porcupine

Intro

Configuration

Configuration

Dealing with Overload

Overload

Automatic Statistics Injection

Overload

##Installation

        <dependency>
            <groupId>com.airhacks</groupId>
            <artifactId>porcupine</artifactId>
            <version>NEWEST_VERSION</version>
            <scope>compile</scope>
        </dependency>

With statistics injection into HTTP headers of all JAX-RS resources:

        <dependency>
            <groupId>com.airhacks</groupId>
            <artifactId>porcupine-spy</artifactId>
            <version>NEWEST_VERSION</version>
            <scope>compile</scope>
        </dependency>

##Conventional Usage

@Stateless
public class MessagesService {

    @Inject
    @Dedicated
    ExecutorService light;

    @Inject
    @Dedicated
    ExecutorService heavy;

Custom naming is also supported:

    @Inject
    @Dedicated("custom-pool")
    private ExecutorService first;

##Statistics and monitoring

###Exposure

@Path("statistics")
@RequestScoped
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public class StatisticsResource {

    @Inject
    Instance<List<Statistics>> statistics;

	@GET
    public List<Statistics> expose() {
        return this.statistics.get();
	}

}

###Sample output

Unified thread pool statistics are accessible via injection. Individual statistics are injectable with the @Dedicated qualifier.

XML statistics

<statistics>
	<pipelineName>light</pipelineName>
	<remainingQueueCapacity>100</remainingQueueCapacity>
	<completedTaskCount>1</completedTaskCount>
	<activeThreadCount>0</activeThreadCount>
	<largestThreadPoolSize>1</largestThreadPoolSize>
	<currentThreadPoolSize>1</currentThreadPoolSize>
	<totalNumberOfTasks>1</totalNumberOfTasks>
	<maximumPoolSize>48</maximumPoolSize>
	<rejectedExecutionHandlerName>ExecutorServiceExposer$$Lambda$3/1562863014</rejectedExecutionHandlerName>
	<rejectedTasks>0</rejectedTasks>
</statistics>
<statistics>
	<pipelineName>heavy</pipelineName>
	<remainingQueueCapacity>16</remainingQueueCapacity>
	<completedTaskCount>0</completedTaskCount>
	<activeThreadCount>1</activeThreadCount>
	<largestThreadPoolSize>1</largestThreadPoolSize>
	<currentThreadPoolSize>1</currentThreadPoolSize>
	<totalNumberOfTasks>1</totalNumberOfTasks>
	<maximumPoolSize>8</maximumPoolSize>
	<rejectedExecutionHandlerName>CallerRunsPolicy</rejectedExecutionHandlerName>
	<rejectedTasks>0</rejectedTasks>
</statistics>

JSON statistics

[{
        "pipelineName": "light",
        "remainingQueueCapacity": 100,
        "completedTaskCount": 1,
        "activeThreadCount": 0,
        "largestThreadPoolSize": 1,
        "currentThreadPoolSize": 1,
        "totalNumberOfTasks": 1,
        "maximumPoolSize": 48,
        "rejectedExecutionHandlerName": "ExecutorServiceExposer$$Lambda$3/1562863014",
        "rejectedTasks": 0
    },
    {
        "pipelineName": "heavy",
        "remainingQueueCapacity": 16,
        "completedTaskCount": 1,
        "activeThreadCount": 0,
        "largestThreadPoolSize": 1,
        "currentThreadPoolSize": 1,
        "totalNumberOfTasks": 1,
        "maximumPoolSize": 8,
        "rejectedExecutionHandlerName": "CallerRunsPolicy",
        "rejectedTasks": 0
    }]

Custom configuration (fully optional)

@Specializes
public class CustomExecutorConfigurator extends ExecutorConfigurator {

    @Override
    public ExecutorConfiguration defaultConfigurator() {
        return super.defaultConfigurator();
    }

    @Override
    public ExecutorConfiguration forPipeline(String name) {
        if ("heavy".equals(name)) {
            return new ExecutorConfiguration.Builder().
                    corePoolSize(4).
                    maxPoolSize(8).
                    queueCapacity(16).
                    keepAliveTime(1).
                    callerRunsPolicy().
                    build();
        }
        return super.forPipeline(name);
    }

}

Statistics Injection into HTTP Headers

HTTP/1.1 200 OK
Server: GlassFish Server Open Source Edition  4.1 
X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  4.1  Java/Oracle Corporation/1.8)
x-porcupine-statistics-light: {"pipelineName":"light","activeThreadCount":1,"completedTaskCount":1,
"corePoolSize":8,"currentThreadPoolSize":2,"largestThreadPoolSize":2,"maximumPoolSize":16,"rejectedTasks":0,
"remainingQueueCapacity":100,"minQueueCapacity":100,"totalNumberOfTasks":2}
x-porcupine-statistics-heavy: {"pipelineName":"heavy","rejectedExecutionHandlerName":"CallerRunsPolicy",
"activeThreadCount":0,"completedTaskCount":1,"corePoolSize":4,"currentThreadPoolSize":1,
"largestThreadPoolSize":1,"maximumPoolSize":8,"rejectedTasks":0,"remainingQueueCapacity":16,
"minQueueCapacity":16,"totalNumberOfTasks":1}
Content-Type: text/plain
Date: Wed, 18 Mar 2015 07:50:20 GMT
Content-Length: 36

porcupine's People

Contributors

adambien avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

porcupine's Issues

Jakarta EE support

Adam,
Is there any interest in updating this to support Jakarta EE? I did a quick and dirty migration updating the imports and temporarily commenting out all the tests and it appears to run fine in Jakarta EE 10.

cannot override porcupine thread factory by @Specializes

Hi Adam,

I am not able to override porcupine default configuration. I wrote this class:


import com.airhacks.porcupine.configuration.control.ExecutorConfigurator;
import com.airhacks.porcupine.execution.control.ExecutorConfiguration;

import javax.enterprise.inject.Specializes;

@Specializes
public class CustomExecutorConfigurator extends ExecutorConfigurator {

    @Override
    public ExecutorConfiguration defaultConfigurator() {
        return super.defaultConfigurator();
    }

    @Override
    public ExecutorConfiguration forPipeline(String name) {
        return new ExecutorConfiguration.Builder().
                corePoolSize(4).
                maxPoolSize(83).
                queueCapacity(16).
                keepAliveTime(1).
                callerRunsPolicy().
                build();
    }

}

but when I hit statistics endpoint a I see still default configuration.

 {
        "pipelineName": "archiveBrowsableDays-endpoint-executor",
        "remainingQueueCapacity": 100,
        "minQueueCapacity": 100,
        "completedTaskCount": 0,
        "activeThreadCount": 0,
        "largestThreadPoolSize": 0,
        "currentThreadPoolSize": 0,
        "totalNumberOfTasks": 0,
        "maximumPoolSize": 16,
        "rejectedExecutionHandlerName": "ExecutorServiceExposer$$Lambda$3957/1578815540",
        "rejectedTasks": 0,
        "corePoolSize": 8
    },
    {
        "pipelineName": "init-endpoint-executor",
        "remainingQueueCapacity": 100,
        "minQueueCapacity": 100,
        "completedTaskCount": 1,
        "activeThreadCount": 0,
        "largestThreadPoolSize": 1,
        "currentThreadPoolSize": 1,
        "totalNumberOfTasks": 1,
        "maximumPoolSize": 16,
        "rejectedExecutionHandlerName": "ExecutorServiceExposer$$Lambda$3957/1578815540",
        "rejectedTasks": 0,
        "corePoolSize": 8
    }

What I do wrong? Thanks.

Security Context propgation into Runnables

I was using porcupine to create an ExecutorService in my project. When I would send in my first round of Runnables into the executor everything worked as expected. I then tried added another round of Runnables into the executor and was running into permissions issues. I checked the logs and the second round of runnables were running as the original user. I had to use a ManagedExecutorService instead to get the SessionContext to properly propagate into the Runnables. I wrote a stackoverflow question that provides a more clear example: http://stackoverflow.com/questions/40002010/how-to-avoid-executorservice-from-overridding-security-principal-of-a-runnable

Are the ExecutorService and the generated threads supposed to be container manager or is that an invalid assumption that I made?

Custom Rejection Policy doesn't update statistics

I'm not sure if this is by design, but when I use a "custom" reject policy i.e. .discard or abort then the porpupine statistics module doesn't register the rejection.

This is obviously because the onRejectedExecution method is never called.

Can we have the cookie and eat it too? Provide different rejection handler and see it in the stats :)

Redeploy issue on glassfish

deploying a maven project to glassfish (payara 1.4.151) (from NetBeans) and doing a mvn clean afterwards resulting in the following message:
Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.4.1:clean (default-clean) on project api-server-web: Failed to clean project: Failed to delete ....\WEB-INF\lib\porcupine-0.0.4.jar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.