Search in sources :

Example 1 with Queue

use of com.google.cloud.tasks.v2.Queue in project algorithms-sedgewick-wayne by reneargento.

the class Exercise7 method keyIndexedCountWithQueue.

public void keyIndexedCountWithQueue(String[] array, int stringsLength) {
    // Extended ASCII characters
    int alphabetSize = 256;
    Queue<String>[] count = new Queue[alphabetSize + 1];
    for (int r = 0; r < count.length; r++) {
        count[r] = new Queue();
    }
    for (int digit = stringsLength - 1; digit >= 0; digit--) {
        // Compute frequency counts
        for (int i = 0; i < array.length; i++) {
            int digitIndex = array[i].charAt(digit);
            count[digitIndex].enqueue(array[i]);
        }
        // Distribute and copy back
        int indexArray = 0;
        for (int r = 0; r < count.length; r++) {
            while (!count[r].isEmpty()) {
                String string = count[r].dequeue();
                array[indexArray++] = string;
            }
        }
    }
}
Also used : Queue(chapter1.section3.Queue)

Example 2 with Queue

use of com.google.cloud.tasks.v2.Queue in project algorithms-sedgewick-wayne by reneargento.

the class Exercise41_CopyQueue method main.

public static void main(String[] args) {
    Queue<Integer> originalQueue = new Queue<>();
    originalQueue.enqueue(1);
    originalQueue.enqueue(2);
    originalQueue.enqueue(3);
    originalQueue.enqueue(4);
    Exercise41_CopyQueue<Integer> queueCopy = new Exercise41_CopyQueue<>(originalQueue);
    queueCopy.enqueue(5);
    queueCopy.enqueue(99);
    originalQueue.dequeue();
    queueCopy.dequeue();
    queueCopy.dequeue();
    StringJoiner originalQueueItems = new StringJoiner(" ");
    for (int item : originalQueue) {
        originalQueueItems.add(String.valueOf(item));
    }
    StdOut.println("Original Queue: " + originalQueueItems.toString());
    StdOut.println("Expected: 2 3 4");
    StdOut.println();
    StringJoiner copyQueueItems = new StringJoiner(" ");
    for (int item : queueCopy) {
        copyQueueItems.add(String.valueOf(item));
    }
    StdOut.println("Queue Copy: " + copyQueueItems.toString());
    StdOut.println("Expected: 3 4 5 99");
}
Also used : Queue(edu.princeton.cs.algs4.Queue) StringJoiner(java.util.StringJoiner)

Example 3 with Queue

use of com.google.cloud.tasks.v2.Queue in project workbench by all-of-us.

the class ForwardingCloudTasksStub method createTaskCallable.

@Override
public UnaryCallable<CreateTaskRequest, Task> createTaskCallable() {
    return new UnaryCallable<CreateTaskRequest, Task>() {

        @Override
        public ApiFuture<Task> futureCall(CreateTaskRequest request, ApiCallContext context) {
            final QueueName queueName = QueueName.parse(request.getParent());
            final AppEngineHttpRequest gaeReq = request.getTask().getAppEngineHttpRequest();
            final Request apiReq = new Request.Builder().url(baseUrl + gaeReq.getRelativeUri()).headers(Headers.of(gaeReq.getHeadersMap())).addHeader("X-AppEngine-QueueName", queueName.getQueue()).post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), gaeReq.getBody().toStringUtf8())).build();
            log.info(String.format("asynchronously forwarding task request for queue '%s', to handler '%s'", queueName.getQueue(), apiReq.url()));
            OkHttpClient client = new OkHttpClient();
            client.setReadTimeout(10, TimeUnit.MINUTES);
            client.newCall(apiReq).enqueue(new Callback() {

                @Override
                public void onFailure(Request request, IOException e) {
                    log.log(Level.SEVERE, "task execution failed", e);
                }

                @Override
                public void onResponse(Response response) {
                }
            });
            return ApiFutures.immediateFuture(request.getTask());
        }
    };
}
Also used : Task(com.google.cloud.tasks.v2.Task) OkHttpClient(com.squareup.okhttp.OkHttpClient) UnaryCallable(com.google.api.gax.rpc.UnaryCallable) CreateTaskRequest(com.google.cloud.tasks.v2.CreateTaskRequest) AppEngineHttpRequest(com.google.cloud.tasks.v2.AppEngineHttpRequest) Request(com.squareup.okhttp.Request) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) IOException(java.io.IOException) AppEngineHttpRequest(com.google.cloud.tasks.v2.AppEngineHttpRequest) Response(com.squareup.okhttp.Response) Callback(com.squareup.okhttp.Callback) CreateTaskRequest(com.google.cloud.tasks.v2.CreateTaskRequest) QueueName(com.google.cloud.tasks.v2.QueueName)

Example 4 with Queue

use of com.google.cloud.tasks.v2.Queue in project java-docs-samples by GoogleCloudPlatform.

the class CreateQueue method createQueue.

public static void createQueue(String projectId, String locationId, String queueBlueName, String queueRedName) throws Exception {
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // TODO(developer): Uncomment these lines and replace with your values.
        // String projectId = "your-project-id";
        // String locationId = "us-central1";
        // String queueBlueName = "queue-blue";
        // String queueRedName = "queue-red";
        LocationName parent = LocationName.of(projectId, locationId);
        Queue queueBlue = Queue.newBuilder().setName(QueueName.of(projectId, locationId, queueBlueName).toString()).setRateLimits(RateLimits.newBuilder().setMaxDispatchesPerSecond(5.0)).setAppEngineRoutingOverride(AppEngineRouting.newBuilder().setVersion("v2").setService("task-module")).build();
        Queue queueRed = Queue.newBuilder().setName(QueueName.of(projectId, locationId, queueRedName).toString()).setRateLimits(RateLimits.newBuilder().setMaxDispatchesPerSecond(1.0)).build();
        Queue[] queues = new Queue[] { queueBlue, queueRed };
        for (Queue queue : queues) {
            Queue response = client.createQueue(parent, queue);
            System.out.println(response);
        }
    }
}
Also used : CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) Queue(com.google.cloud.tasks.v2.Queue) LocationName(com.google.cloud.tasks.v2.LocationName)

Example 5 with Queue

use of com.google.cloud.tasks.v2.Queue in project java-docs-samples by GoogleCloudPlatform.

the class PauseQueue method pauseQueue.

public static void pauseQueue(String projectId, String locationId, String queueId) throws Exception {
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // TODO(developer): Uncomment these lines and replace with your values.
        // String projectId = "your-project-id";
        // String locationId = "us-central1";
        // String queueId = "foo";
        // Construct the fully qualified queue name.
        String queueName = QueueName.of(projectId, locationId, queueId).toString();
        client.pauseQueue(queueName);
        System.out.println("Queue Paused.");
    }
}
Also used : CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient)

Aggregations

CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)10 Task (com.google.cloud.tasks.v2.Task)8 In (edu.princeton.cs.algs4.In)5 Queue (edu.princeton.cs.algs4.Queue)5 Queue (chapter1.section3.Queue)4 LocationName (com.google.cloud.tasks.v2.LocationName)3 Queue (com.google.cloud.tasks.v2.Queue)3 ByteString (com.google.protobuf.ByteString)3 Test (org.junit.jupiter.api.Test)3 HashSet (chapter3.section5.HashSet)2 AppEngineHttpRequest (com.google.cloud.tasks.v2.AppEngineHttpRequest)2 Transaction (edu.princeton.cs.algs4.Transaction)2 StringJoiner (java.util.StringJoiner)2 UnionFind (chapter1.section5.UnionFind)1 PriorityQueueResize (chapter2.section4.PriorityQueueResize)1 SeparateChainingHashTable (chapter3.section4.SeparateChainingHashTable)1 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)1 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)1 CreateTaskRequest (com.google.cloud.tasks.v2.CreateTaskRequest)1 QueueName (com.google.cloud.tasks.v2.QueueName)1