
org.glassfish.concurrent.admin.ManagedExecutorServiceBaseManager Maven / Gradle / Ivy
/*
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.concurrent.admin;
import com.sun.enterprise.config.serverbeans.BindableResource;
import com.sun.enterprise.config.serverbeans.Resource;
import com.sun.enterprise.config.serverbeans.Resources;
import com.sun.enterprise.util.LocalStringManagerImpl;
import jakarta.inject.Inject;
import jakarta.resource.ResourceException;
import java.beans.PropertyVetoException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.naming.SimpleJndiName;
import org.glassfish.concurrent.config.ManagedExecutorService;
import org.glassfish.concurrent.config.ManagedExecutorServiceBase;
import org.glassfish.concurrent.config.ManagedScheduledExecutorService;
import org.glassfish.config.support.CommandTarget;
import org.glassfish.resourcebase.resources.admin.cli.ResourceUtil;
import org.glassfish.resourcebase.resources.api.ResourceStatus;
import org.glassfish.resourcebase.resources.util.BindableResourcesHelper;
import org.glassfish.resources.admin.cli.ResourceManager;
import org.jvnet.hk2.config.ConfigSupport;
import org.jvnet.hk2.config.SingleConfigCode;
import org.jvnet.hk2.config.TransactionFailure;
import org.jvnet.hk2.config.types.Property;
import static com.sun.enterprise.config.serverbeans.ServerTags.DESCRIPTION;
import static com.sun.enterprise.config.serverbeans.ServerTags.MANAGED_EXECUTOR_SERVICE;
import static com.sun.enterprise.config.serverbeans.ServerTags.MANAGED_SCHEDULED_EXECUTOR_SERVICE;
import static com.sun.enterprise.deployment.xml.ConcurrencyTagNames.CONTEXT_INFO;
import static com.sun.enterprise.deployment.xml.ConcurrencyTagNames.CONTEXT_INFO_DEFAULT_VALUE;
import static com.sun.enterprise.deployment.xml.ConcurrencyTagNames.CONTEXT_INFO_ENABLED;
import static com.sun.enterprise.deployment.xml.ConcurrencyTagNames.CORE_POOL_SIZE;
import static com.sun.enterprise.deployment.xml.ConcurrencyTagNames.HUNG_AFTER_SECONDS;
import static com.sun.enterprise.deployment.xml.ConcurrencyTagNames.HUNG_LOGGER_INITIAL_DELAY_SECONDS;
import static com.sun.enterprise.deployment.xml.ConcurrencyTagNames.HUNG_LOGGER_INTERVAL_SECONDS;
import static com.sun.enterprise.deployment.xml.ConcurrencyTagNames.HUNG_LOGGER_PRINT_ONCE;
import static com.sun.enterprise.deployment.xml.ConcurrencyTagNames.KEEP_ALIVE_SECONDS;
import static com.sun.enterprise.deployment.xml.ConcurrencyTagNames.LONG_RUNNING_TASKS;
import static com.sun.enterprise.deployment.xml.ConcurrencyTagNames.THREAD_LIFETIME_SECONDS;
import static com.sun.enterprise.deployment.xml.ConcurrencyTagNames.THREAD_PRIORITY;
import static org.glassfish.resources.admin.cli.ResourceConstants.ENABLED;
import static org.glassfish.resources.admin.cli.ResourceConstants.JNDI_NAME;
import static org.glassfish.resources.admin.cli.ResourceConstants.SYSTEM_ALL_REQ;
/**
*
* The base managed executor service manager for managed executor service
* and managed scheduled executor service
*/
public abstract class ManagedExecutorServiceBaseManager implements ResourceManager {
protected static final LocalStringManagerImpl I18N = new LocalStringManagerImpl(
ManagedExecutorServiceBaseManager.class);
protected String jndiName;
protected String description;
protected String threadPriority = Integer.toString(Thread.NORM_PRIORITY);
protected String contextInfoEnabled = Boolean.TRUE.toString();
protected String contextInfo = CONTEXT_INFO_DEFAULT_VALUE;
protected String longRunningTasks = Boolean.FALSE.toString();
protected String hungAfterSeconds = "0";
protected String hungLoggerPrintOnce = Boolean.FALSE.toString();
protected String hungLoggerInitialDelaySeconds = "60";
protected String hungLoggerIntervalSeconds = "60";
protected String corePoolSize = "0";
protected String keepAliveSeconds = "60";
protected String threadLifetimeSeconds = "0";
protected String enabled = Boolean.TRUE.toString();
protected String enabledValueForTarget = Boolean.TRUE.toString();
@Inject
protected ResourceUtil resourceUtil;
@Inject
protected ServerEnvironment environment;
@Inject
protected BindableResourcesHelper resourcesHelper;
@Override
public abstract String getResourceType();
@Override
public ResourceStatus create(Resources resources, HashMap attributes, final Properties properties, String target)
throws Exception {
setAttributes(attributes, target);
ResourceStatus validationStatus = isValid(resources, true, target);
if(validationStatus.getStatus() == ResourceStatus.FAILURE){
return validationStatus;
}
try {
ConfigSupport.apply(param -> createResource(param, properties), resources);
if (!CommandTarget.TARGET_DOMAIN.equals(target)) {
resourceUtil.createResourceRef(jndiName, enabledValueForTarget, target);
}
} catch (TransactionFailure tfe) {
String msg = I18N.getLocalString("create.managed.executor.service.failed",
"Managed executor service {0} creation failed", jndiName) + tfe.getLocalizedMessage();
if (MANAGED_SCHEDULED_EXECUTOR_SERVICE.equals(getResourceType())) {
msg = I18N.getLocalString("create.managed.scheduled.executor.service.failed",
"Managed scheduled executor service {0} creation failed", jndiName) + tfe.getLocalizedMessage();
}
ResourceStatus status = new ResourceStatus(ResourceStatus.FAILURE, msg);
status.setException(tfe);
return status;
}
String msg = I18N.getLocalString("create.managed.executor.service.success",
"Managed executor service {0} created successfully", jndiName);
if (MANAGED_SCHEDULED_EXECUTOR_SERVICE.equals(getResourceType())) {
msg = I18N.getLocalString("create.managed.scheduled.executor.service.success",
"Managed scheduled executor service {0} created successfully", jndiName);
}
return new ResourceStatus(ResourceStatus.SUCCESS, msg);
}
protected ResourceStatus isValid(Resources resources, boolean validateResourceRef, String target){
if (jndiName == null) {
String msg = I18N.getLocalString("managed.executor.service.noJndiName",
"No JNDI name defined for managed executor service.");
if (MANAGED_SCHEDULED_EXECUTOR_SERVICE.equals(getResourceType())) {
msg = I18N.getLocalString("managed.scheduled.executor.service.noJndiName",
"No JNDI name defined for managed scheduled executor service.");
}
return new ResourceStatus(ResourceStatus.FAILURE, msg);
}
final Class extends BindableResource> clazz;
if (MANAGED_SCHEDULED_EXECUTOR_SERVICE.equals(getResourceType())) {
clazz = ManagedScheduledExecutorService.class;
} else {
clazz = ManagedExecutorService.class;
}
return resourcesHelper.validateBindableResourceForDuplicates(resources, jndiName, validateResourceRef, target, clazz);
}
protected void setAttributes(Map attributes, String target) {
jndiName = attributes.get(JNDI_NAME);
description = attributes.get(DESCRIPTION);
contextInfo = attributes.get(CONTEXT_INFO);
contextInfoEnabled = attributes.get(CONTEXT_INFO_ENABLED);
threadPriority = attributes.get(THREAD_PRIORITY);
longRunningTasks = attributes.get(LONG_RUNNING_TASKS);
hungAfterSeconds = attributes.get(HUNG_AFTER_SECONDS);
hungLoggerPrintOnce = attributes.get(HUNG_LOGGER_PRINT_ONCE);
hungLoggerInitialDelaySeconds = attributes.get(HUNG_LOGGER_INITIAL_DELAY_SECONDS);
hungLoggerIntervalSeconds = attributes.get(HUNG_LOGGER_INTERVAL_SECONDS);
corePoolSize = attributes.get(CORE_POOL_SIZE);
keepAliveSeconds = attributes.get(KEEP_ALIVE_SECONDS);
threadLifetimeSeconds = attributes.get(THREAD_LIFETIME_SECONDS);
if (target == null) {
enabled = attributes.get(ENABLED);
} else {
enabled = resourceUtil.computeEnabledValueForResourceBasedOnTarget(attributes.get(ENABLED), target);
}
enabledValueForTarget = attributes.get(ENABLED);
}
protected ManagedExecutorServiceBase createResource(Resources param, Properties properties)
throws PropertyVetoException, TransactionFailure {
ManagedExecutorServiceBase newResource = createConfigBean(param, properties);
param.getResources().add(newResource);
return newResource;
}
protected abstract ManagedExecutorServiceBase createConfigBean(Resources param, Properties properties)
throws PropertyVetoException, TransactionFailure;
protected void setAttributesOnConfigBean(ManagedExecutorServiceBase managedExecutorService, Properties properties) throws PropertyVetoException, TransactionFailure {
managedExecutorService.setJndiName(jndiName);
if (description != null) {
managedExecutorService.setDescription(description);
}
managedExecutorService.setContextInfoEnabled(contextInfoEnabled);
managedExecutorService.setContextInfo(contextInfo);
managedExecutorService.setThreadPriority(threadPriority);
managedExecutorService.setHungAfterSeconds(hungAfterSeconds);
managedExecutorService.setHungLoggerPrintOnce(hungLoggerPrintOnce);
managedExecutorService.setHungLoggerInitialDelaySeconds(hungLoggerInitialDelaySeconds);
managedExecutorService.setHungLoggerIntervalSeconds(hungLoggerIntervalSeconds);
managedExecutorService.setCorePoolSize(corePoolSize);
managedExecutorService.setKeepAliveSeconds(keepAliveSeconds);
managedExecutorService.setThreadLifetimeSeconds(threadLifetimeSeconds);
managedExecutorService.setEnabled(enabled);
managedExecutorService.setLongRunningTasks(longRunningTasks);
if (properties != null) {
for (Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy