Skip to content

Wicket 6930 websocket improvements#479

Merged
reiern70 merged 1 commit into
wicket-9.xfrom
WICKET-6930-websocket-improvements
Nov 11, 2021
Merged

Wicket 6930 websocket improvements#479
reiern70 merged 1 commit into
wicket-9.xfrom
WICKET-6930-websocket-improvements

Conversation

@reiern70
Copy link
Copy Markdown
Contributor

@reiern70 reiern70 commented Nov 7, 2021

This PR adds some extra functionality to WS implementation. Not sure if this can be done in a cleaner way or a more general one. This adds.

  • Allows to avoid sending "empty" web socket push messages (i.e. no components or scripts are added to it). The use case is user starts a background task and component listening for it are no longer in page. Thus events still go through a page but nothing is added to them.
  • The pageClass is added as a variable to WS page related key. Use case? You want to deliver to a class of pages... or you want to start a task and leave a page, or open a new page and still receive events..
  • I modified the examples to illustrate this.

@reiern70
Copy link
Copy Markdown
Contributor Author

reiern70 commented Nov 7, 2021

@martin-g Not very sure about the pageCLass I have introduced. Maybe call this "context"? i.e. page class is just a. context?

Copy link
Copy Markdown
Member

@martin-g martin-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the new pageClass attribute:

  • PageIdKey is no more accurate name, since it is not just the id anymore. But renaming it would be an API break
  • What if someone wants to do the same with a resource based WebSocket behavior ?

IMO we should move this logic at IKey level.
And if we can avoid class loading it would be better, but I am not sure how exactly to do it yet!

progressUpdateTask.cancel();
}

public static JSR356Session getInstance() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/getInstance/get/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return;
}
ScheduledExecutorService service = JSR356Application.get().getScheduledExecutorService();
progressUpdateTask = ProgressUpdater.start(getApplication(), getId(), service);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can cache the value of JSR356Application.get() and reuse it instead of calling getApplication()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

image

add(new WebSocketBehavior()
{
@Override
protected void onConnect(ConnectedMessage message) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this override is not really needed here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

progressUpdateTask = ProgressUpdater.start(getApplication(), getId(), service);
}

public synchronized void cancel() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/cancel/cancelTask/

ProgressUpdater.ProgressUpdateTask progressUpdateTask = JSR356Session.getInstance().getProgressUpdateTask();
if (progressUpdateTask != null && progressUpdateTask.isRunning() && !progressUpdateTask.isCanceled())
{
progressUpdateTask.cancel();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify it to JSR356Session.getInstance().cancelTask();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. Done.

interface IConnectionsFilter
{

boolean acceptConnection(String sessionId, IKey key);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to accept(...) ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

this.pageId = Args.notNull(pageId, "pageId");
Args.notNull(pageClass, "pageClass");
try {
this.pageClass = (Class<IManageablePage>)Thread.currentThread().getContextClassLoader().loadClass(pageClass);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use IClassResolver

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have refactored code. Now context is just an String and tis is done on Updater class

image

This is a background thread and application is not in thread context.

try {
this.pageClass = (Class<IManageablePage>)Thread.currentThread().getContextClassLoader().loadClass(pageClass);
} catch (ClassNotFoundException e) {
//throw new WicketRuntimeException(e);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look good! Most probably we should not ignore the problem.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. No longer pertinent.

* The name of the request parameter that holds the application name
*/
private static final String WICKET_APP_PARAM_NAME = "wicket-app-name";
private static final String WICKET_SESSION_ID = "jsessionid";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not used

public void onOpen(Session session, EndpointConfig endpointConfig)
{
String appName = getApplicationName(session);
session.getId();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum... Got rid of this

@reiern70
Copy link
Copy Markdown
Contributor Author

reiern70 commented Nov 8, 2021

@martin-g many thanks for your comments! I will go through them right now

@reiern70
Copy link
Copy Markdown
Contributor Author

reiern70 commented Nov 8, 2021

About the new pageClass attribute:

  • PageIdKey is no more accurate name, since it is not just the id anymore. But renaming it would be an API break
  • What if someone wants to do the same with a resource based WebSocket behavior ?

Good point. This is why I thought about context. Maybe call this context and move this to a common Ikey?

IMO we should move this logic at IKey level. And if we can avoid class loading it would be better, but I am not sure how exactly to do it yet!

Ok. I will give it a try.

@reiern70
Copy link
Copy Markdown
Contributor Author

reiern70 commented Nov 9, 2021

@martin-g I have moved context to IKey. See

74bea3b

@reiern70 reiern70 requested a review from martin-g November 10, 2021 08:21
import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.request.Request;

public class JSR356Session extends WebSession {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The opening curly bracket should be on a new line.

@reiern70 reiern70 requested a review from martin-g November 11, 2021 07:09
@reiern70
Copy link
Copy Markdown
Contributor Author

@martin-g Many thanks for review and useful comments! I will squash commits and merge to wicket 9.x.

@martin-g
Copy link
Copy Markdown
Member

And cherry-pick the final commit to master please!

…ages 2) pass page class as a kind of context that can be used to send a web socket to different pages + example of updating a component in a given page (not dependent on page ID)
@reiern70 reiern70 force-pushed the WICKET-6930-websocket-improvements branch from dd443bc to 04aaa7f Compare November 11, 2021 10:00
@reiern70 reiern70 merged commit 04aaa7f into wicket-9.x Nov 11, 2021
@reiern70
Copy link
Copy Markdown
Contributor Author

And cherry-pick the final commit to master please!

Done! Thanks again for your help!

@solomax solomax deleted the WICKET-6930-websocket-improvements branch April 28, 2022 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants