ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/tomcat/websocket/BackgroundProcessManager.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/tomcat/websocket/BackgroundProcessManager.java
Status
scanned
Type
file
Name
BackgroundProcessManager.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
4625 (4.5 KB)
MD5
42f07d35c492ddcc0244ff9bb90b744e
SHA1
c397b4eb4534c597aa408b25f4fc97c1ea8caab5
SHA256
4b5ef44dfa7da63d7119f1b69f8ed493335ce86f424664ad74745f9aa9c44ccb
SHA512

      
    
SHA1_git
0914669809801f00eacd8738bf14e73719f902a6
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
BackgroundProcessManager.java | 4.5 KB |

/* * 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.apache.tomcat.websocket; import java.util.HashSet; import java.util.Set; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; import org.apache.tomcat.util.ExceptionUtils; import org.apache.tomcat.util.res.StringManager; /** * Provides a background processing mechanism that triggers roughly once a second. The class maintains a thread that * only runs when there is at least one instance of {@link BackgroundProcess} registered. */ public class BackgroundProcessManager { private final Log log = LogFactory.getLog(BackgroundProcessManager.class); private static final StringManager sm = StringManager.getManager(BackgroundProcessManager.class); private static final BackgroundProcessManager instance; static { instance = new BackgroundProcessManager(); } public static BackgroundProcessManager getInstance() { return instance; } private final Set<BackgroundProcess> processes = new HashSet<>(); private final Object processesLock = new Object(); private WsBackgroundThread wsBackgroundThread = null; private BackgroundProcessManager() { // Hide default constructor } public void register(BackgroundProcess process) { synchronized (processesLock) { if (processes.isEmpty()) { wsBackgroundThread = new WsBackgroundThread(this); wsBackgroundThread.setContextClassLoader(this.getClass().getClassLoader()); wsBackgroundThread.setDaemon(true); wsBackgroundThread.start(); } processes.add(process); } } public void unregister(BackgroundProcess process) { synchronized (processesLock) { processes.remove(process); if (wsBackgroundThread != null && processes.isEmpty()) { wsBackgroundThread.halt(); wsBackgroundThread = null; } } } private void process() { Set<BackgroundProcess> currentProcesses; synchronized (processesLock) { currentProcesses = new HashSet<>(processes); } for (BackgroundProcess process : currentProcesses) { try { process.backgroundProcess(); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); log.error(sm.getString("backgroundProcessManager.processFailed"), t); } } } /* * For unit testing. */ int getProcessCount() { synchronized (processesLock) { return processes.size(); } } void shutdown() { synchronized (processesLock) { processes.clear(); if (wsBackgroundThread != null) { wsBackgroundThread.halt(); wsBackgroundThread = null; } } } private static class WsBackgroundThread extends Thread { private final BackgroundProcessManager manager; private volatile boolean running = true; WsBackgroundThread(BackgroundProcessManager manager) { setName("WebSocket background processing"); this.manager = manager; } @Override public void run() { while (running) { try { sleep(1000); } catch (InterruptedException e) { // Ignore } manager.process(); } } public void halt() { setName("WebSocket background processing - stopping"); running = false; } } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
30.75
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9