From ee836db2ea6e7f3f76789233df3f344f28cf2f53 Mon Sep 17 00:00:00 2001 From: Jacques Le Roux Date: Tue, 28 Jun 2016 18:50:04 +0000 Subject: [PATCH] A patch from Amardeep Singh Jhajj for "Add websocket support in OFBiz" https://issues.apache.org/jira/browse/OFBIZ-7073 It also contains a slightly modified patch from Amardeep Singh Jhajj for "WebSocket Example - Push Notifications" - https://issues.apache.org/jira/browse/OFBIZ-7467 - It's adding working example (in example application) of WebSocket support I tried to use websockets in OFBiz. I simply added tomcat-embed-websocket.jar in catalina lib and created one webapp for websocket and also added server endpoint class. It didn't work. After that, I tried the same thing with plain j2ee application with embedded tomcat. It worked there. I researched on above issue in OFBiz and got the reason. Websockets implementation need jar scanning enabled and it is currently disabled in OFBiz. Below is the code snippet of disabling jar scan from CatalinaContainer.java: JarScanner jarScanner = context.getJarScanner(); if (jarScanner instanceof StandardJarScanner) { StandardJarScanner standardJarScanner = (StandardJarScanner) jarScanner; standardJarScanner.setScanClassPath(false); } Jar scanning enabling increase OFBiz server startup time upto couples of minutes (in my case, it took approx 8 minutes), so we don't want this much of startup time for OFBiz. I got the following document where I found the reason why websocket is not working if scanning disabled. https://wiki.apache.org/tomcat/HowTo/FasterStartUp Here tips are given to decrease the startup time. This tips also include disabling of jar scanning. We can say disabling jar scanning is right approach because if we enable it then scanner will scan all the jars loaded in OFBiz startup that we don't want. But, If we want websockets working then we have to enable jar scanning. For enabling jar scanning, we need below code: standardJarScanner.setScanClassPath(true); // Will increase server startup time. Solution: We can add filter on jar scanning. It will allow only some kind of jars only. For example: jars having websockets endpoints. I am attaching patch for the same here. I added filter like if jar name string contains "discoverable" word then only it will be considered for jar scan. We can change jar name of our jars using build.xml to make it discoverable for jar scanning. For example: I have added my websocket endpoint class in "specialpurpose/ecommerce/src" and changed the "name" property in build.xml of ecommerce component from "ofbiz-ecommerce" to "ofbiz-ecommerce-discoverable". Here is the code snippet from build.xml: This change will create the jar with name "ofbiz-ecommerce-discoverable.jar" in "ecommerce/build/lib/". Now created jar will be scanned in jar scanner as its name contains "discoverable" word in it. This change will not increase server start up time more than couple of seconds (in my case, it just two seconds). So scanning time totally depends on the list of jars scanned. Conclusion: We can use websocket support with the help of jar filters. The very basic working example of sending push notifications to all connected clients using WebSocket added by Amardeep Singh Jhajj works as follow: 1. Adds support to show push notifications over every screen of an example application using WebSocket when any example created or updated. 2. As it's an example, so for now push notifications will only for example creation and update using SECA's. 3. Push notification will be shown on screen for 5 seconds only, we can close it before 5 seconds using close button if needed. The UI of notification pop-up can be changed. I kept it simple (No fancy work) to just demonstrate the WebSocket usage. To see it in action, Please follow the steps given in OFBIZ-7073 and apply patch given here. jleroux: I committed the 2 patches at the same time because the changes in .classpath and the LICENSE files were in the example patch for OFBIZ-7467. I also slightly modified the example patch in order to provide an example where no sync issues are possible by using a Collections.synchronizedSet for the javax.websocket.Session Set. I also used synchronized loops as it's requested for synchronizedSet where it's used. I know it rather symbolic in the example component but it will remember developers about the possible sync issue. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk/specialpurpose@1750557 13f79535-47bb-0310-9956-ffa450edef68 --- example/build.xml | 3 +- example/servicedef/secas.xml | 10 +++ example/servicedef/services.xml | 7 ++ .../org/ofbiz/example/ExampleServices.java | 49 ++++++++++++ .../org/ofbiz/example/ExampleWebSockets.java | 79 +++++++++++++++++++ example/webapp/example/WEB-INF/web.xml | 2 +- .../example/js/ExamplePushNotifications.js | 67 ++++++++++++++++ example/widget/example/CommonScreens.xml | 8 ++ 8 files changed, 223 insertions(+), 2 deletions(-) create mode 100644 example/src/org/ofbiz/example/ExampleServices.java create mode 100644 example/src/org/ofbiz/example/ExampleWebSockets.java create mode 100644 example/webapp/example/js/ExamplePushNotifications.js diff --git a/example/build.xml b/example/build.xml index 73fe77cb7..fdd103f14 100644 --- a/example/build.xml +++ b/example/build.xml @@ -27,7 +27,7 @@ under the License. - + @@ -36,6 +36,7 @@ under the License. + diff --git a/example/servicedef/secas.xml b/example/servicedef/secas.xml index 706e434e7..e6a1a2d5f 100644 --- a/example/servicedef/secas.xml +++ b/example/servicedef/secas.xml @@ -31,4 +31,14 @@ under the License. --> + + + + + + + + + + diff --git a/example/servicedef/services.xml b/example/servicedef/services.xml index b7ab6df9f..d44d21555 100644 --- a/example/servicedef/services.xml +++ b/example/servicedef/services.xml @@ -126,4 +126,11 @@ under the License. location="component://example/minilang/example/ExampleServices.xml" invoke="testCreateExampleService"> + + + + + + diff --git a/example/src/org/ofbiz/example/ExampleServices.java b/example/src/org/ofbiz/example/ExampleServices.java new file mode 100644 index 000000000..c20b714d8 --- /dev/null +++ b/example/src/org/ofbiz/example/ExampleServices.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.example; + +import java.io.IOException; +import java.util.Map; +import java.util.Set; + +import javax.websocket.Session; + +import org.ofbiz.base.util.Debug; +import org.ofbiz.service.DispatchContext; +import org.ofbiz.service.ServiceUtil; + +public class ExampleServices { + public static final String module = ExampleServices.class.getName(); + + public static Map sendExamplePushNotifications(DispatchContext dctx, Map context) { + String exampleId = (String) context.get("exampleId"); + String message = (String) context.get("message"); + Set clients = (Set) ExampleWebSockets.getClients(); + try { + synchronized (clients) { + for (Session client : clients) { + client.getBasicRemote().sendText(message + ": " + exampleId); + } + } + } catch (IOException e) { + Debug.logError(e.getMessage(), module); + } + return ServiceUtil.returnSuccess(); + } +} \ No newline at end of file diff --git a/example/src/org/ofbiz/example/ExampleWebSockets.java b/example/src/org/ofbiz/example/ExampleWebSockets.java new file mode 100644 index 000000000..4bed2fbd9 --- /dev/null +++ b/example/src/org/ofbiz/example/ExampleWebSockets.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ + +package org.ofbiz.example; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import javax.websocket.OnClose; +import javax.websocket.OnMessage; +import javax.websocket.OnOpen; +import javax.websocket.Session; +import javax.websocket.server.ServerEndpoint; + +import org.ofbiz.base.util.Debug; + +@ServerEndpoint("/ws/pushNotifications") +public class ExampleWebSockets { + + public static final String module = ExampleWebSockets.class.getName(); + private static Set clients = Collections.synchronizedSet(new HashSet()); + + + @OnMessage + public void onMessage(Session session, String msg, boolean last) { + try { + if (session.isOpen()) { + synchronized (clients) { + for(Session client : clients){ + if (!client.equals(session)){ + client.getBasicRemote().sendText(msg); + } + } + } + } + } catch (IOException e) { + try { + session.close(); + } catch (IOException ioe) { + Debug.logError(ioe.getMessage(), module); + } + } + } + + @OnOpen + public void onOpen (Session session) { + // Add session to the connected sessions clients set + clients.add(session); + } + + @OnClose + public void onClose (Session session) { + // Remove session from the connected sessions clients set + clients.remove(session); + } + + public static Set getClients () { + return clients; + } +} + diff --git a/example/webapp/example/WEB-INF/web.xml b/example/webapp/example/WEB-INF/web.xml index fee6eba22..e22b479a6 100644 --- a/example/webapp/example/WEB-INF/web.xml +++ b/example/webapp/example/WEB-INF/web.xml @@ -49,7 +49,7 @@ under the License. disableContextSecurityN allowedPaths - /error:/control:/select:/index.html:/index.jsp:/default.html:/default.jsp:/images:/includes/maincss.css + /error:/control:/select:/index.html:/index.jsp:/default.html:/default.jsp:/images:/includes/maincss.css:/js:/ws errorCode403 redirectPath/control/main diff --git a/example/webapp/example/js/ExamplePushNotifications.js b/example/webapp/example/js/ExamplePushNotifications.js new file mode 100644 index 000000000..5859a8c5f --- /dev/null +++ b/example/webapp/example/js/ExamplePushNotifications.js @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +var webSocket; + +// Create a new instance of the websocket +webSocket = new WebSocket('wss://' + window.location.host + '/example/ws/pushNotifications'); + +webSocket.onopen = function(event){ + // Do any operation on open +}; + +webSocket.onmessage = function(event) { + + // Remove already present notification + jQuery('.pushNotification').remove(); + + // Create notification on the fly, this notification markup can be added in Messages.ftl and its css can be theme based. + jQuery("body") + .append(jQuery('
') + .addClass('pushNotification') + .css({'position': 'fixed', 'top': '5%', 'right': '2%', 'background': 'lightgrey', 'border-radius': '10px', 'z-index': '9999'})); + + jQuery('.pushNotification') + .append(jQuery('') + .css({'color': 'black', 'font-size': '15px', 'position': 'fixed', 'right': '2%'}).append('close')); + + jQuery('.pushNotification') + .append(jQuery('

') + .addClass('msg') + .css({'font-size': '15px', 'padding': '30px 20px 30px 20px'})); + + jQuery('.pushNotification').find('.msg').append(event.data); + + // show notification + jQuery('.pushNotification').fadeIn(); + + // Remove notification after 5 seconds + setTimeout(function() { + jQuery('.pushNotification').remove(); + }, 5000 ); + + // Added observer for close link. + jQuery('.closeNotification').click(function() { + jQuery(this).parent('.pushNotification').remove(); + }); +}; + +webSocket.onerror = function(event){ + // Do any operation on error +}; diff --git a/example/widget/example/CommonScreens.xml b/example/widget/example/CommonScreens.xml index cb876b9b2..1336d71f7 100644 --- a/example/widget/example/CommonScreens.xml +++ b/example/widget/example/CommonScreens.xml @@ -45,6 +45,14 @@ under the License. +

+ + + + + + +