Skip to content

Commit

Permalink
Issue jetty#2578 - Listener behavior cleanup (Jetty 10.0.x) (jetty#3966)
Browse files Browse the repository at this point in the history
* Avoid creating listener list for rarely used requestAttributeListener

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#3964

Keep a list of EventListeners in the AbstractConnector to make it
more efficient to add and iterate over them.

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#2578 EventListener

Use addEventListener rather than bespoke listener methods.
Support getEventListenerBeans at Container level for fast lookup
improve javadoc

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#2578 EventListener

fixed test
more javadoc

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#2578 EventListener

fixed tests

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#3964

Don't use null for empty lists of listeners

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* fix merge

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#2578 EventListener

Resolve differences between eventListeners added as beans and beans
added as EventListeners.   The behaviour should now be the same
regardless of how they listener is added and all listeners are now
beans.

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#2578 EventListener

Add only SelectorManager listeners to manager from connector

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#2578 EventListener

Fixed javadoc

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#2578 EventListener

removed old TODO

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#2578 EventListener

connector cannot be null

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#2578 EventListener

javadoc

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#3964

AbstractConnector keeps a specific list of HttpChannel.Listeners
to avoid Connection.Listeners and MBean listeners being added to
the HttpChannel listener list.

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#2578 EventListener

fixed merge

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* fixed javadoc

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#2578 EventListener

removed the ability to set/clear context listeners
Instead just remove non-durable ones.

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#3964 Listeners

Simplified listener handling by avoiding null connector, previously
only needed for testing.

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* fixed bad merge

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#3964 Listeners

Fixed test that assumed HttpChannel listeners were not cleared by a recycle

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#3964 Listeners

Separated out durable vs cyclic HttpChannel.Listeners, so as to
simplify handling.

Deprecated cyclic HttpChannel.Listeners, as I'm not sure the channel is
the right place for them.

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* removed deprecated cyclic HttpChannel listeners

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* removed deprecated cyclic HttpChannel listeners - import

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue jetty#4003 Cleanup quickstart

* Fixed tests that scan for "Started" on console

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* updates from review

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw authored Nov 13, 2019
1 parent 774160c commit 46a3368
Show file tree
Hide file tree
Showing 65 changed files with 429 additions and 372 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.security.PermissionCollection;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -615,9 +614,9 @@ public void doStop()
TaskLog.logWithTimestamp("Stopping web application " + this);
Thread.currentThread().sleep(500L);
super.doStop();
//remove all filters, servlets and listeners. They will be recreated
//either via application of a context xml file or web.xml or annotation or servlet api
setEventListeners(new EventListener[0]);
// remove all filters and servlets. They will be recreated
// either via application of a context xml file or web.xml or annotation or servlet api.
// Event listeners are reset in ContextHandler.doStop()
getServletHandler().setFilters(new FilterHolder[0]);
getServletHandler().setFilterMappings(new FilterMapping[0]);
getServletHandler().setServlets(new ServletHolder[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public void testHttpClientName() throws Exception
String name = "foo";
HttpClient httpClient = new HttpClient();
httpClient.setName(name);
httpClient.start();

try
{
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
MBeanContainer mbeanContainer = new MBeanContainer(mbeanServer);
// Adding MBeanContainer as a bean will trigger the registration of MBeans.
httpClient.addBean(mbeanContainer);
httpClient.start();

String domain = HttpClient.class.getPackage().getName();
ObjectName pattern = new ObjectName(domain + ":type=" + HttpClient.class.getSimpleName().toLowerCase(Locale.ENGLISH) + ",*");
Expand Down
2 changes: 1 addition & 1 deletion jetty-home/src/main/resources/etc/jetty-setuid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!-- ================================================================ -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.setuid.SetUIDListener">
<Set name="startServerAsPrivileged"><Property name="jetty.setuid.startServerAsPrivileged" default="false"/></Set>
Expand Down
2 changes: 1 addition & 1 deletion jetty-home/src/main/resources/etc/jetty-started.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!-- Mixin the Start FileNoticeLifeCycleListener -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.util.component.FileNoticeLifeCycleListener">
<Arg><Property name="jetty.state" default="./jetty.state"/></Arg>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Connection newConnection(EndPoint endPoint, Map<String, Object> context)

final HTTP2ClientConnection connection = new HTTP2ClientConnection(client, byteBufferPool, executor, endPoint,
parser, session, client.getInputBufferSize(), promise, listener);
connection.addListener(connectionListener);
connection.addEventListener(connectionListener);
return customize(connection, context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public Connection newConnection(Connector connector, EndPoint endPoint)
endPoint, httpConfiguration, parser, session, getInputBufferSize(), listener);
connection.setUseInputDirectByteBuffers(isUseInputDirectByteBuffers());
connection.setUseOutputDirectByteBuffers(isUseOutputDirectByteBuffers());
connection.addListener(sessionContainer);
connection.addEventListener(sessionContainer);
return configure(connection, connector, endPoint);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.eclipse.jetty.io;

import java.util.EventListener;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -57,13 +58,14 @@ protected AbstractConnection(EndPoint endp, Executor executor)
}

@Override
public void addListener(Listener listener)
public void addEventListener(EventListener listener)
{
_listeners.add(listener);
if (listener instanceof Listener)
_listeners.add((Listener)listener);
}

@Override
public void removeListener(Listener listener)
public void removeEventListener(EventListener listener)
{
_listeners.remove(listener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.eclipse.jetty.io;

import java.io.IOException;
import java.util.EventListener;
import java.util.List;
import java.util.Map;

Expand All @@ -43,7 +44,7 @@ public default Connection customize(Connection connection, Map<String, Object> c
{
ContainerLifeCycle client = (ContainerLifeCycle)context.get(CLIENT_CONTEXT_KEY);
if (client != null)
client.getBeans(Connection.Listener.class).forEach(connection::addListener);
client.getBeans(EventListener.class).forEach(connection::addEventListener);
return connection;
}

Expand Down
7 changes: 4 additions & 3 deletions jetty-io/src/main/java/org/eclipse/jetty/io/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.Closeable;
import java.nio.ByteBuffer;
import java.util.EventListener;

import org.eclipse.jetty.util.component.Container;

Expand All @@ -38,14 +39,14 @@ public interface Connection extends Closeable
*
* @param listener the listener to add
*/
public void addListener(Listener listener);
public void addEventListener(EventListener listener);

/**
* <p>Removes a listener of connection events.</p>
*
* @param listener the listener to remove
*/
public void removeListener(Listener listener);
public void removeEventListener(EventListener listener);

/**
* <p>Callback method invoked when this connection is opened.</p>
Expand Down Expand Up @@ -133,7 +134,7 @@ public interface UpgradeTo
* the Connector or ConnectionFactory are added as listeners to all new connections
* </p>
*/
public interface Listener
public interface Listener extends EventListener
{
public void onOpened(Connection connection);

Expand Down
47 changes: 29 additions & 18 deletions jetty-io/src/main/java/org/eclipse/jetty/io/SelectorManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.jetty.util.ProcessorUtils;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.Container;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.Dumpable;
import org.eclipse.jetty.util.log.Log;
Expand Down Expand Up @@ -389,31 +390,37 @@ protected void connectionFailed(SelectableChannel channel, Throwable ex, Object
*/
public abstract Connection newConnection(SelectableChannel channel, EndPoint endpoint, Object attachment) throws IOException;

public void addEventListener(EventListener listener)
/**
* @param listener An EventListener
* @see AcceptListener
* @see Container#addEventListener(EventListener)
*/
@Override
public boolean addEventListener(EventListener listener)
{
if (isRunning())
throw new IllegalStateException(this.toString());
if (listener instanceof AcceptListener)
addAcceptListener(AcceptListener.class.cast(listener));
if (super.addEventListener(listener))
{
if (listener instanceof AcceptListener)
_acceptListeners.add((AcceptListener)listener);
return true;
}
return false;
}

public void removeEventListener(EventListener listener)
@Override
public boolean removeEventListener(EventListener listener)
{
if (isRunning())
throw new IllegalStateException(this.toString());
if (listener instanceof AcceptListener)
removeAcceptListener(AcceptListener.class.cast(listener));
}

public void addAcceptListener(AcceptListener listener)
{
if (!_acceptListeners.contains(listener))
_acceptListeners.add(listener);
}

public void removeAcceptListener(AcceptListener listener)
{
_acceptListeners.remove(listener);
if (super.removeEventListener(listener))
{
if (listener instanceof AcceptListener)
_acceptListeners.remove(listener);
return true;
}
return false;
}

protected void onAccepting(SelectableChannel channel)
Expand Down Expand Up @@ -461,12 +468,16 @@ protected void onAccepted(SelectableChannel channel)
}
}

public interface SelectorManagerListener extends EventListener
{
}

/**
* <p>A listener for accept events.</p>
* <p>This listener is called from either the selector or acceptor thread
* and implementations must be non blocking and fast.</p>
*/
public interface AcceptListener extends EventListener
public interface AcceptListener extends SelectorManagerListener
{
/**
* Called immediately after a new SelectableChannel is accepted, but
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,8 @@ public String toString()
}

/**
* <p>A listener of {@link WriteFlusher} events.</p>
* <p>A listener of {@link WriteFlusher} events.
* If implemented by a Connection class, the {@link #onFlushed(long)} event will be delivered to it.</p>
*/
public interface Listener
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
</New>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Ref id="httpConnector">
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -356,11 +355,9 @@ public void doStop() throws Exception

super.doStop();

// remove all listeners, servlets and filters. This is because we will
// re-apply
// any context xml file, which means they would potentially be added
// multiple times.
setEventListeners(new EventListener[0]);
// remove all servlets and filters. This is because we will
// re-appy any context xml file, which means they would potentially be
// added multiple times.
getServletHandler().setFilters(new FilterHolder[0]);
getServletHandler().setFilterMappings(new FilterMapping[0]);
getServletHandler().setServlets(new ServletHolder[0]);
Expand Down
2 changes: 1 addition & 1 deletion jetty-maven-plugin/src/main/resources/jetty-maven.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">

<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerListener">
<Set name="tokenFile"><Property name="jetty.token.file"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void start(BundleContext context) throws Exception
{
//For test purposes, use a random port
Server server = new Server(0);
server.getConnectors()[0].addLifeCycleListener(new AbstractLifeCycleListener()
server.getConnectors()[0].addEventListener(new AbstractLifeCycleListener()
{

/**
Expand Down
Loading

0 comments on commit 46a3368

Please sign in to comment.