JavaCro'14 - Using WildFly core to build high performance web server – Tomaž Cerar

Post on 13-Jan-2015

1,390 views 2 download

Tags:

description

WildFly core is fully modular application server, which is used as base to build WildFly EE container and much more. Functionalities such as EE are implemented as sets of extensions also known as subsystems. Extensions give you low level access to application server’s functionalities such as JBoss Modules for class loading Domain management model Deployment processors Modular Service Container (aka service kernel)

transcript

Using WildFly core to build high

performance web server

Tomaž Cerar, Red Hat

Agenda

• Undertow

• WildFly core

• What are extensions

• Write simple extension• Write simple extension

• Undertow extension

• Demo

WILDFLY CORE

WildFly core

• Really small (< 15 mb)

• Consists of

– JBoss Modules

– Modular Service Controller (MSC)– Modular Service Controller (MSC)

– Domain management (CLI, rest)

– Deployment manager

– Logging

• Base for WildFly 8, EE7 certified app server

JBoss Modules

• Modular class loader

• Concurrent class loader implementation

• Isolated class loader

• Each module consists of set of resources• Each module consists of set of resources

Domain management

• Manages configuration

• Backbone for all extensions

• Accessible via

– Extensions– Extensions

– CLI

– Admin console

– DMR clients

– …

Modular Service Controller

• Truly concurrent service container

• Handles service dependencies

• Handles lifecycle

• All “real” functionality should be in services• All “real” functionality should be in services

DMR

• Detyped Model Representation

• JSON like

• Internal format for all operations

• Internal model (ModelNode)• Internal model (ModelNode)

• Resides in org.jboss.dmr package

• Really small

UNDERTOW

Undertow

• Highly concurrent web server

• Servlet 3.1 container

• HTTP / AJP / SSL Proxy server

• Supports Websocket implementation• Supports Websocket implementation

• Supports HTTP Upgrade

• Supports use of high performance non-blocking

handlers in servlet deployments

• Initial HTTP2 / Spdy support

Why Another Webserver?

• A true non-blocking API (servlet is by itself

insufficient)

• Improved support for modern security standards

• Upgrade from HTTP to our native protocols• Upgrade from HTTP to our native protocols

• Allows WildFly to run in single port mode

• Support the latest network protocols naturally and

with minimal overhead

• Best possible performance and memory efficiency

• Handle complex routing use cases a la mod_rewrite

• Embeddable in standalone process / test environment

What are extensions?

• Entry point for extending WildFly

• Can provide

– new deployment types

– new model– new model

– services

– resources

Domain model definition

• Attributes

• Resources

• Resource tree

– PathElement– PathElement

– Single target (key=value)

– Multi target (key=*)

• Resolvers & multi language support

Operation handlers

• Defines operations on resources

• Execution stage

– MODEL

– RUNTIME– RUNTIME

– VERIFY

– DOMAIN

– DONE

• Can be chained

Resource handlers

• Every resource needs add & remove

• In MODEL phase

– validate and set model

• In RUNTIME phase• In RUNTIME phase

– Add services

– Add deployment processors

Deployment manager

• Deployment repository

• Defines deployment phases

• Provides deployment processor infrastructure

• Can be used via• Can be used via

– Domain management

– Deployment scanner

Deployment processor

• Hooks into deployment lifecycle

• Can modify deployment behavior

• Can define new deployment type

Extension point

• Loaded via Service Loader

• Can define many subsystems

• Packaged as JBoss Module

• Referenced in configuration• Referenced in configuration

• Can provide any new functionality

Extension loading

standalone.xml<extension module="org.wildfly.extension.sdd"/>

ServiceLoaderorg.jboss.as.controller.Extension

org.wildfly.extension.sdd.SDDExtension

WRITING EXTENSIONS

Building blocks

• Define Extension point

• Define Root Model

• Define Add handler

• Define XML parser • Define XML parser

• Define XML marshaller

Extension class

public class UndertowExtension implements Extension {@Overridepublic void initializeParsers(ExtensionParsingContext context) {

}

@Override@Overridepublic void initialize(ExtensionContext context) {

}}

Service Loader entry

• Point it to implementation class

– org.wildfly.extension.undertow.UndertowExtension

Define root modelpublic class UndertowRootDefinition extends PersistentResourceDefinition {

protected static final SimpleAttributeDefinition STATISTICS_ENABLED =

new SimpleAttributeDefinitionBuilder("statistics-enabled", ModelType.BOOLEAN, true)

.setAllowExpression(true)

.setDefaultValue(new ModelNode(false))

.build();

static final UndertowRootDefinition INSTANCE = new UndertowRootDefinition ();static final UndertowRootDefinition INSTANCE = new UndertowRootDefinition ();

private SDDRootResource() {

super(UndertowExtension.SUBSYSTEM_PATH,

UndertowExtension.getResolver(),

UndertowSubsystemAdd.INSTANCE,

ReloadRequiredRemoveStepHandler.INSTANCE);

}

@Override

public Collection<AttributeDefinition> getAttributes() {

return Arrays.asList(ATTRIBUTES);

}

}

Define root add handler

public class UndertowSubsystemAdd extends AbstractBoottimeAddStepHandler {

@Override

protected void populateModel(ModelNode operation, ModelNode model) throws OperationFailedException

{

for (AttributeDefinition def : UndertowRootDefinition. .INSTANCE.getAttributes()) {

def.validateAndSet(operation, model);

}}

}

Define namespace

enum Namespace {

// must be first

UNKNOWN(null),

UNDERTOW_1_0("urn:jboss:domain:undertow:1.0");

/**

* The current namespace version.* The current namespace version.

*/

public static final Namespace CURRENT = UNDERTOW_1_1;

private final String name;

Namespace(final String name) {

this.name = name;

}

}

Define XML model

<subsystem xmlns="urn:wildfly:domain:undertow:1.0“ statistics-enabled=“true”/>

Define parserclass UndertowSubsystemParser_1_0 implements XMLStreamConstants,

XMLElementReader<List<ModelNode>> {

static final PersistentResourceXMLDescription xmlDescription =

builder(UndertowRootDefinition.INSTANCE).build();

@Override@Override

public void readElement(XMLExtendedStreamReader reader, List<ModelNode>

list) throws XMLStreamException {

xmlDescription.parse(reader, PathAddress.EMPTY_ADDRESS, list);

}

}

Define marshaller

public void writeContent(XMLExtendedStreamWriter writer, SubsystemMarshallingContext context) throws

XMLStreamException {

xmlDescription.persist(writer, context.getModel(), Namespace.CURRENT.getUriString());

}

Making it all work

@Override

public void initializeParsers(ExtensionParsingContext context) {

context.setSubsystemXmlMapping(SUBSYSTEM_NAME, Namespace.UNDERTOW_1_0.getUriString(),

UndertowSubsystemParser_1_0.INSTANCE);

}

@Override

public void initialize(ExtensionContext context) {public void initialize(ExtensionContext context) {

final SubsystemRegistration subsystem =

context.registerSubsystem(SUBSYSTEM_NAME, 1, 0, 0);

final ManagementResourceRegistration registration =

subsystem.registerSubsystemModel(UnertowRootResource.INSTANCE);

subsystem.registerXMLElementWriter(UndertowSubsystemParser.INSTANCE);

}

Module definition<module xmlns="urn:jboss:module:1.3" name="org.wildfly.extension.undertoworg.wildfly.extension.undertoworg.wildfly.extension.undertoworg.wildfly.extension.undertow">

<resources><artifact name="${org.wildfly:wildfly-undertow}"/>

</resources><dependencies><module name="io.undertow.core" services="import"/>

<module name="io.undertow.servlet" services="import"/><module name="io.undertow.jsp"/><module name="io.undertow.websocket"/><module name="javax.api"/><module name="javax.api"/><module name="javax.servlet.api"/><module name="javax.servlet.jsp.api"/><module name="javax.websocket.api"/><module name="org.jboss.jandex"/><module name="org.jboss.staxmapper"/><module name="org.jboss.as.controller"/><module name="org.jboss.as.server"/><module name="org.jboss.modules"/><module name="org.jboss.msc"/><module name="org.jboss.vfs"/><module name="org.jboss.logging"/>

</dependencies></module>

What does it do?

(almost) nothing!

How to test it?

• Test harness enables you to test

– xml parser

– model consistency

– MSC services

– Compatibility (transformers)

– Localization resources

– …

public class UndertowSubsystemTestCase extends AbstractSubsystemBaseTest {

public UndertowSubsystemTestCase() {super(“undertow”, new UndertowExtension());super(“undertow”, new UndertowExtension());

}

@Overrideprotected String getSubsystemXml() throws IOException {

return readResource(“undertow-1.0.xml");}

}

Undertow extension

• Multiple servers & hosts per server support

• Each server can have multiple listeners

• Support AJP, HTTP, HTTPS listeners

• Servlet & WebSockets deployments support• Servlet & WebSockets deployments support

• SSO

• Session Management

• Proxy & LB mangament

• Filters & content handlers

• Predicates

• ….

Demo

• http://undertow.io/

• https://github.com/wildfly/wildfly

• http://www.wildfly.org/

Questions?