Skip to content

Commit

Permalink
Merged branch '5.0.x' into '6.0.x'.
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
  • Loading branch information
sbordet committed Jul 28, 2022
2 parents 7b19257 + 7b7d08d commit 136d455
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cometd-demo/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<!-- Request that Jetty create an MBean to manage the Bayeux instance -->
<context-param>
<param-name>org.eclipse.jetty.server.context.ManagedAttributes</param-name>
<param-value>org.cometd.bayeux,org.cometd.oort.Oort</param-value>
<param-value>org.cometd.bayeux,org.cometd.oort.Oort,org.cometd.oort.Seti</param-value>
</context-param>

<!-- Filter to support cross domain requests -->
Expand Down
14 changes: 11 additions & 3 deletions cometd-documentation/src/main/asciidoc/extensions_reload.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ cometd.reload({

The reload extension is useful to allow users to reload CometD pages, or to navigate to other CometD pages, without going through a disconnect and handshake cycle, thus resuming an existing CometD session on the reloaded or on the new page.

When reloading or navigating away from a page, browsers will destroy the JavaScript context associated to that page, and interrupt the connection to the server too.
On the reloaded or on the new page, the JavaScript context is recreated anew by the browser, but the CometD JavaScript library has lost all the CometD session details that were established in the previous page.
In absence of the reload extension, application need to go through another handshake step to recreate the CometD session details needed.
When reloading or navigating away from a page, the browser destroys the JavaScript context associated to that page, and interrupts the connection(s) to the server.
On the reloaded or on the new page, the JavaScript context is recreated anew by the browser, new connections are opened to the server, but the CometD JavaScript library has lost all the CometD session details that were established in the previous page.
In absence of the reload extension, the application needs to go through another handshake step to recreate the CometD session details needed.

The reload extension gives the ability to resume the CometD session in the new page, by re-establishing the previously successful CometD session.
This is useful especially when the server builds a stateful context for the CometD session that is not to be lost just because the user refreshed the page, or navigated to another part of the same CometD web application.
Expand All @@ -91,6 +91,14 @@ When `cometd.reload()` is called, the reload extension saves the CometD session
When the new page loads up, it will execute the same code executed on the first page load, namely the code that configured CometD, that registered channel listeners, and that finally called `cometd.handshake()`.
The reload extension is invoked upon the new handshake, will find the CometD session state saved in the `SessionStorage` and will re-establish the CometD session with the information retrieved from the `SessionStorage`.

[CAUTION]
====
If the CometD server is configured with xref:_java_server_configuration_transports[`ws.requireHandshakePerConnection`] set to `true`, and the communication with the server happens via WebSocket, then the reload extension will not work.
This is because when the new page loads up, the browser will open a new WebSocket connection and the server will require a new handshake for this new connection.
The new handshake will create a different CometD session, and the CometD session before the reload is lost because it was associated with the previous handshake.
====

[NOTE]
====
Function `cometd.reload()` should be called from the page _unload_ event handler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ Here is the list of configuration init parameters (to be specified in `web.xml`)
| ws.requireHandshakePerConnection
| false
| Whether every new WebSocket connection requires a handshake, see xref:_security[the security section].
When set to `true`, and the communication with the server happens via WebSocket, the xref:_extensions_reload[reload extension] will not work.

| ws.enableExtension.<extension_name>
| true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ protected void sweep(long now) {
lock.lock();
try {
if (_expireTime == 0) {
if (_maxProcessing > 0 && now > _messageTime + _maxProcessing) {
if (_maxProcessing > 0 && (now - _messageTime) > _maxProcessing) {
_logger.info("Sweeping during processing {}", this);
remove = true;
}
} else {
if (now > _expireTime) {
if (now - _expireTime > 0) {
if (_logger.isDebugEnabled()) {
_logger.debug("Sweeping {}", this);
}
Expand Down Expand Up @@ -1053,34 +1053,37 @@ public String toString() {
long last;
long expire;
State state;
int size;
long now = System.nanoTime();
lock.lock();
try {
cycle = getMetaConnectCycle();
last = now - _messageTime;
expire = _expireTime == 0 ? 0 : _expireTime - now;
state = _state;
size = _queue.size();
} finally {
lock.unlock();
}
return String.format("%s@%x[%s,%s,cycle=%d,last=%d,expire=%d]",
return String.format("%s@%x[%s,%s,q=%d,cycle=%d,last=%d,expire=%d]",
getClass().getSimpleName(),
hashCode(),
_id,
state,
size,
cycle,
TimeUnit.NANOSECONDS.toMillis(last),
TimeUnit.NANOSECONDS.toMillis(expire));
}

private class LazyTask implements Runnable {
private long _execution;
private long _nextExecutionNanos;
private volatile Task _task;

@Override
public void run() {
flush();
_execution = 0;
_nextExecutionNanos = 0;
_task = null;
}

Expand All @@ -1090,10 +1093,11 @@ public boolean cancel() {
}

public boolean schedule(long lazyTimeout) {
long execution = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(lazyTimeout);
if (_task == null || execution < _execution) {
long nextExecutionNanos = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(lazyTimeout);
// Reschedule if the next execution is earlier than the previous one.
if (_task == null || (_nextExecutionNanos - nextExecutionNanos) > 0) {
cancel();
_execution = execution;
_nextExecutionNanos = nextExecutionNanos;
_task = _bayeux.schedule(this, lazyTimeout);
return true;
}
Expand Down
20 changes: 10 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@
<!-- end::jetty-version[] -->
<graalvm-version>22.2.0</graalvm-version>
<slf4j-version>2.0.0-alpha7</slf4j-version>
<log4j2-version>2.17.2</log4j2-version>
<spring-version>5.3.21</spring-version>
<spring-boot-version>2.7.0</spring-boot-version>
<log4j2-version>2.18.0</log4j2-version>
<spring-version>5.3.22</spring-version>
<spring-boot-version>2.7.2</spring-boot-version>
<jackson-version>2.13.3</jackson-version>
<dojo-version>1.17.2</dojo-version>
<okhttp-version>4.10.0</okhttp-version>
Expand Down Expand Up @@ -345,7 +345,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<version>3.4.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -365,7 +365,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M2</version>
<version>3.0.0</version>
<configuration>
<retryFailedDeploymentCount>8</retryFailedDeploymentCount>
</configuration>
Expand All @@ -383,7 +383,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
<version>3.0.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -410,7 +410,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<version>3.3.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -465,12 +465,12 @@
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>5.1.6</version>
<version>5.1.7</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
Expand Down Expand Up @@ -529,7 +529,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down

0 comments on commit 136d455

Please sign in to comment.