Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2021 Google LLC
*
* Licensed 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
*
* https://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 com.google.appengine.api.taskqueue.dev;
import com.google.appengine.api.taskqueue.InternalFailureException;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueConstants;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddRequest;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueAddResponse;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddRequest;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueBulkAddResponse;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueDeleteRequest;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueDeleteResponse;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueFetchQueueStatsRequest;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueFetchQueueStatsResponse;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueMode.Mode;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueModifyTaskLeaseRequest;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueModifyTaskLeaseResponse;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueuePurgeQueueRequest;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueuePurgeQueueResponse;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksRequest;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueScannerQueueInfo;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueServiceError.ErrorCode;
import com.google.appengine.api.urlfetch.URLFetchServicePb;
import com.google.appengine.api.urlfetch.dev.LocalURLFetchService;
import com.google.appengine.tools.development.AbstractLocalRpcService;
import com.google.appengine.tools.development.ApiUtils;
import com.google.appengine.tools.development.Clock;
import com.google.appengine.tools.development.LatencyPercentiles;
import com.google.appengine.tools.development.LocalRpcService;
import com.google.appengine.tools.development.LocalServerEnvironment;
import com.google.appengine.tools.development.LocalServiceContext;
import com.google.apphosting.api.ApiProxy;
import com.google.apphosting.utils.config.ConfigurationException;
import com.google.apphosting.utils.config.QueueXml;
import com.google.apphosting.utils.config.QueueXmlReader;
import com.google.apphosting.utils.config.QueueYamlReader;
import com.google.auto.service.AutoService;
import com.google.appengine.repackaged.com.google.protobuf.ByteString;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
/**
* A local implementation of the Task Queue service interface backed by Quartz
* (http://www.opensymphony.com/quartz). This class is responsible for managing the lifecycle of the
* Quartz {@link Scheduler} but otherwise delegates to {@link DevQueue} for all the scheduling
* intelligence.
*
*/
@AutoService(LocalRpcService.class)
public final class LocalTaskQueue extends AbstractLocalRpcService {
private static final Logger logger = Logger.getLogger(LocalTaskQueue.class.getName());
private static final String DOC_LINK =
"https://cloud.google.com/appengine/docs/standard/java/config/queueref-yaml";
/** The package name for this service. */
public static final String PACKAGE = "taskqueue";
/**
* The name of a property that disables automatic task execution. If this property exists and is
* set to true in the {@code properties} argument to {@link #init} then the schedule will not
* automatically run any tasks. Manual task execution will still work as normal.
*/
public static final String DISABLE_AUTO_TASK_EXEC_PROP = "task_queue.disable_auto_task_execution";
/**
* Overrides the path of queue.xml. Must be a full path, e.g. /usr/local/dev/myapp/tests/queue.xml
*/
public static final String QUEUE_XML_PATH_PROP = "task_queue.queue_xml_path";
/**
* Overrides the path of queue.yaml. Must be a full path, e.g.
* /usr/local/dev/myapp/tests/queue.yaml
*/
public static final String QUEUE_YAML_PATH_PROP = "task_queue.queue_yaml_path";
/**
* Overrides the {@link LocalTaskQueueCallback} class that is used to service async task
* execution. The value of this property must be the fully-qualified name of a class with a
* public, no-arg constructor that implements the {@link LocalTaskQueueCallback} interface.
*/
public static final String CALLBACK_CLASS_PROP = "task_queue.callback_class";
/** Collection of queues mapped by queue name, sorted by queue name. */
private final Map queues =
// Using a TreeMap to get deterministic ordering.
Collections.synchronizedMap(new TreeMap());
private QueueXml queueConfig;
private Scheduler scheduler;
private boolean disableAutoTaskExecution = false;
private LocalServerEnvironment localServerEnvironment;
private Clock clock;
private LocalURLFetchService fetchService;
private LocalTaskQueueCallback callback;
/** Shutdown hook to stop this task queue when the VM exits. */
private Thread shutdownHook;
private Random rng;
@Override
public void init(LocalServiceContext context, Map properties) {
localServerEnvironment = context.getLocalServerEnvironment();
clock = context.getClock();
queueConfig =
parseQueueConfiguration(
localServerEnvironment.getAppDir().getPath(),
properties.get(QUEUE_XML_PATH_PROP),
properties.get(QUEUE_YAML_PATH_PROP));
logger.log(Level.INFO, "LocalTaskQueue is initialized");
if (Boolean.parseBoolean(properties.get(DISABLE_AUTO_TASK_EXEC_PROP))) {
disableAutoTaskExecution = true;
logger.log(Level.INFO, "Automatic task execution is disabled.");
}
fetchService = new LocalURLFetchService();
fetchService.init(null, new HashMap());
// We're only hitting urls of our own app. The app gets 10 minutes
// to process the request so set the url fetch deadline to 10 minutes.
fetchService.setTimeoutInMs(10 * 60 * 1000);
rng = new Random();
initializeCallback(properties);
}
/**
* Parse the queue configuration from application directory, either from xml or yaml configuration
*
* @param appDir the application war directory.
* @param queueXmlPath a user provided path that overrides the default path of queue.xml. This is
* used by local unit tests.
* @param queueYamlPath a user provided path that overrides the default path of queue.yaml. This
* is used by local unit tests.
* @return the parsed queue configuration.
* @throws ConfigurationException if both yaml and xml path are not null.
*/
QueueXml parseQueueConfiguration(
String appDir, @Nullable final String queueXmlPath, @Nullable final String queueYamlPath) {
if (queueXmlPath != null && queueYamlPath != null) {
throw new ConfigurationException(
"Found both queue.xml and queue.yaml. Please use queue.yaml and remove queue.xml. "
+ "For more information: "
+ DOC_LINK);
}
QueueXml resultFromXml = null;
QueueXml resultFromYaml = null;
if (queueXmlPath != null) {
// user wants a custom queue.xml loaded
QueueXmlReader xmlReader =
new QueueXmlReader(appDir) {
@Override
public String getFilename() {
return queueXmlPath;
}
};
resultFromXml = xmlReader.readQueueXml();
} else if (queueYamlPath != null) {
// user wants a custom queue.yaml loaded
QueueYamlReader yamlReader =
new QueueYamlReader(appDir) {
@Override
public String getFilename() {
return queueYamlPath;
}
};
resultFromYaml = yamlReader.parse();
} else { // Tries default path for xml or yaml configuration.
QueueXmlReader xmlReader = new QueueXmlReader(appDir);
// TODO Consider reloading queue.xml if changed.
// resultFromXml would be null iff the xml file does not exist.
resultFromXml = xmlReader.readQueueXml();
QueueYamlReader yamlReader = new QueueYamlReader(Paths.get(appDir, "WEB-INF").toString());
resultFromYaml = yamlReader.parse();
}
if (!ApiUtils.isPromotingYaml()) {
return (resultFromXml != null) ? resultFromXml : resultFromYaml;
}
// When promoting yaml, given proper messages if queue.xml is present.
if (resultFromXml != null && resultFromYaml == null) {
logger.warning(
"Using queue.xml. Please migrate to queue.yaml. For more information: " + DOC_LINK);
}
return (resultFromYaml != null) ? resultFromYaml : resultFromXml;
}
private void initializeCallback(Map properties) {
String callbackOverrideClass = properties.get(CALLBACK_CLASS_PROP);
if (callbackOverrideClass != null) {
// user provided an override for the callback class
try {
callback = (LocalTaskQueueCallback) newInstance(Class.forName(callbackOverrideClass));
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else {
// no override so use the default implementation
callback = new UrlFetchServiceLocalTaskQueueCallback(fetchService);
}
callback.initialize(properties);
}
/**
* Returns a new instance of the provided class, if the provided class has a zero-argument
* constructor. Works even if the class or its constructor is private.
*/
private static E newInstance(Class clazz)
throws InstantiationException, IllegalAccessException {
try {
return clazz.getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException e) {
Constructor defaultConstructor;
try {
defaultConstructor = clazz.getDeclaredConstructor();
} catch (NoSuchMethodException f) {
throw new InstantiationException("No zero-arg constructor.");
}
defaultConstructor.setAccessible(true);
try {
return defaultConstructor.newInstance();
} catch (InvocationTargetException g) {
throw new RuntimeException(g);
}
}
}
// For testing!
void setQueueXml(QueueXml queueXml) {
this.queueConfig = queueXml;
}
@Override
public void start() {
AccessController.doPrivileged(
new PrivilegedAction