Search in sources :

Example 1 with WorkflowServiceStubs

use of io.temporal.serviceclient.WorkflowServiceStubs in project sdk-java by temporalio.

the class RegisterTestNamespace method main.

public static void main(String[] args) throws InterruptedException {
    if (!useDockerService) {
        return;
    }
    WorkflowServiceStubsOptions.Builder options = WorkflowServiceStubsOptions.newBuilder();
    if (serviceAddress != null) {
        options.setTarget(serviceAddress);
    }
    WorkflowServiceStubs service = null;
    try {
        service = WorkflowServiceStubs.newInstance(options.build());
        if (doesNamespaceExist(service)) {
            System.out.println("Namespace " + NAMESPACE + " already exists");
        } else {
            registerNamespace(service);
            waitForNamespace(service);
        }
        System.exit(0);
    } finally {
        if (service != null) {
            service.shutdown();
        }
    }
    System.exit(0);
}
Also used : WorkflowServiceStubsOptions(io.temporal.serviceclient.WorkflowServiceStubsOptions) WorkflowServiceStubs(io.temporal.serviceclient.WorkflowServiceStubs)

Example 2 with WorkflowServiceStubs

use of io.temporal.serviceclient.WorkflowServiceStubs in project sdk-java by temporalio.

the class PollWorkflowTaskDispatcherTests method aWarningIsLoggedAndWorkflowTaskIsFailedWhenNoHandlerIsRegisteredForTheTaskQueue.

@Test
// TODO: Rewrite as mocking of WorkflowServiceBlockingStub is not possible
@Ignore
public void aWarningIsLoggedAndWorkflowTaskIsFailedWhenNoHandlerIsRegisteredForTheTaskQueue() {
    ListAppender<ILoggingEvent> appender = new ListAppender<>();
    appender.setContext(context);
    appender.start();
    logger.addAppender(appender);
    AtomicBoolean handled = new AtomicBoolean(false);
    Functions.Proc1<PollWorkflowTaskQueueResponse> handler = r -> handled.set(true);
    WorkflowServiceGrpc.WorkflowServiceBlockingStub stub = mock(WorkflowServiceGrpc.WorkflowServiceBlockingStub.class);
    WorkflowServiceStubs mockService = mock(WorkflowServiceStubs.class);
    when(mockService.blockingStub()).thenReturn(stub);
    PollWorkflowTaskDispatcher dispatcher = new PollWorkflowTaskDispatcher(mockService, "default", metricsScope);
    dispatcher.subscribe("taskqueue1", handler);
    PollWorkflowTaskQueueResponse response = CreatePollWorkflowTaskQueueResponse("I Don't Exist TaskQueue");
    dispatcher.process(response);
    verify(stub, times(1)).respondWorkflowTaskFailed(any());
    assertFalse(handled.get());
    assertEquals(1, appender.list.size());
    ILoggingEvent event = appender.list.get(0);
    assertEquals(Level.WARN, event.getLevel());
    assertEquals(String.format("No handler is subscribed for the PollWorkflowTaskQueueResponse.WorkflowExecutionTaskQueue %s", "I Don't Exist TaskQueue"), event.getFormattedMessage());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NoopScope(com.uber.m3.tally.NoopScope) LoggerContext(ch.qos.logback.classic.LoggerContext) Functions(io.temporal.workflow.Functions) WorkflowServiceGrpc(io.temporal.api.workflowservice.v1.WorkflowServiceGrpc) After(org.junit.After) Status(io.grpc.Status) TaskQueue(io.temporal.api.taskqueue.v1.TaskQueue) TestCase.assertFalse(junit.framework.TestCase.assertFalse) ListAppender(ch.qos.logback.core.read.ListAppender) Before(org.junit.Before) Logger(org.slf4j.Logger) Scope(com.uber.m3.tally.Scope) WorkflowServiceStubs(io.temporal.serviceclient.WorkflowServiceStubs) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) StatusRuntimeException(io.grpc.StatusRuntimeException) TimeUnit(java.util.concurrent.TimeUnit) Level(ch.qos.logback.classic.Level) Ignore(org.junit.Ignore) TestCase.assertTrue(junit.framework.TestCase.assertTrue) PollWorkflowTaskQueueResponse(io.temporal.api.workflowservice.v1.PollWorkflowTaskQueueResponse) TestWorkflowService(io.temporal.internal.testservice.TestWorkflowService) TestCase.assertEquals(junit.framework.TestCase.assertEquals) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PollWorkflowTaskQueueResponse(io.temporal.api.workflowservice.v1.PollWorkflowTaskQueueResponse) ListAppender(ch.qos.logback.core.read.ListAppender) Functions(io.temporal.workflow.Functions) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) WorkflowServiceGrpc(io.temporal.api.workflowservice.v1.WorkflowServiceGrpc) WorkflowServiceStubs(io.temporal.serviceclient.WorkflowServiceStubs) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with WorkflowServiceStubs

use of io.temporal.serviceclient.WorkflowServiceStubs in project sdk-java by temporalio.

the class MetricsTest method testTemporalInvalidRequestMetric.

@Test
public void testTemporalInvalidRequestMetric() throws InterruptedException {
    setUp(WorkerFactoryOptions.newBuilder().setWorkerInterceptors(new CorruptedSignalWorkerInterceptor()).build());
    try {
        WorkflowServiceStubs serviceStubs = testEnvironment.getWorkflowClient().getWorkflowServiceStubs();
        serviceStubs.blockingStub().startWorkflowExecution(StartWorkflowExecutionRequest.newBuilder().build());
        fail("failure expected");
    } catch (StatusRuntimeException e) {
        assertEquals(Status.Code.INVALID_ARGUMENT, e.getStatus().getCode());
    }
    // Wait for reporter
    Thread.sleep(REPORTING_FLUSH_TIME);
    Map<String, String> tags = new LinkedHashMap<String, String>() {

        {
            putAll(MetricsTag.defaultTags(MetricsTag.DEFAULT_VALUE));
            put(MetricsTag.OPERATION_NAME, "StartWorkflowExecution");
        }
    };
    reporter.assertCounter(TEMPORAL_REQUEST, tags, 1);
    tags.put(MetricsTag.STATUS_CODE, "INVALID_ARGUMENT");
    reporter.assertCounter(TEMPORAL_REQUEST_FAILURE, tags, 1);
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) TestWorkflowReturnString(io.temporal.workflow.shared.TestWorkflows.TestWorkflowReturnString) WorkflowServiceStubs(io.temporal.serviceclient.WorkflowServiceStubs) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 4 with WorkflowServiceStubs

use of io.temporal.serviceclient.WorkflowServiceStubs in project sdk-java by temporalio.

the class MetricsTest method testTemporalFailureMetric.

@Test
public void testTemporalFailureMetric() throws InterruptedException {
    setUp(WorkerFactoryOptions.newBuilder().setWorkerInterceptors(new CorruptedSignalWorkerInterceptor()).build());
    try {
        WorkflowServiceStubs serviceStubs = testEnvironment.getWorkflowClient().getWorkflowServiceStubs();
        serviceStubs.blockingStub().describeNamespace(DescribeNamespaceRequest.newBuilder().build());
        fail("failure expected");
    } catch (StatusRuntimeException e) {
        assertEquals(Status.Code.UNIMPLEMENTED, e.getStatus().getCode());
    }
    // Wait for reporter
    Thread.sleep(REPORTING_FLUSH_TIME);
    Map<String, String> tags = new LinkedHashMap<String, String>() {

        {
            putAll(MetricsTag.defaultTags(MetricsTag.DEFAULT_VALUE));
            put(MetricsTag.OPERATION_NAME, "DescribeNamespace");
        }
    };
    reporter.assertCounter(TEMPORAL_REQUEST, tags, 1);
    tags.put(MetricsTag.STATUS_CODE, "UNIMPLEMENTED");
    reporter.assertCounter(TEMPORAL_REQUEST_FAILURE, tags, 1);
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) TestWorkflowReturnString(io.temporal.workflow.shared.TestWorkflows.TestWorkflowReturnString) WorkflowServiceStubs(io.temporal.serviceclient.WorkflowServiceStubs) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 5 with WorkflowServiceStubs

use of io.temporal.serviceclient.WorkflowServiceStubs in project samples-java by temporalio.

the class HelloAsyncLambda method main.

/**
 * With our Workflow and Activities defined, we can now start execution. The main method starts
 * the worker and then the workflow.
 */
public static void main(String[] args) {
    // Get a Workflow service stub.
    WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
    /*
     * Get a Workflow service client which can be used to start, Signal, and Query Workflow Executions.
     */
    WorkflowClient client = WorkflowClient.newInstance(service);
    /*
     * Define the workflow factory. It is used to create workflow workers for a specific task queue.
     */
    WorkerFactory factory = WorkerFactory.newInstance(client);
    /*
     * Define the workflow worker. Workflow workers listen to a defined task queue and process
     * workflows and activities.
     */
    Worker worker = factory.newWorker(TASK_QUEUE);
    /*
     * Register our workflow implementation with the worker.
     * Workflow implementations must be known to the worker at runtime in
     * order to dispatch workflow tasks.
     */
    worker.registerWorkflowImplementationTypes(GreetingWorkflowImpl.class);
    /**
     * Register our Activity Types with the Worker. Since Activities are stateless and thread-safe,
     * the Activity Type is a shared instance.
     */
    worker.registerActivitiesImplementations(new GreetingActivitiesImpl());
    /*
     * Start all the workers registered for a specific task queue.
     * The started workers then start polling for workflows and activities.
     */
    factory.start();
    // Define our workflow options
    WorkflowOptions workflowOptions = WorkflowOptions.newBuilder().setWorkflowId(WORKFLOW_ID).setTaskQueue(TASK_QUEUE).build();
    // Create the workflow client stub. It is used to start our workflow execution.
    GreetingWorkflow workflow = client.newWorkflowStub(GreetingWorkflow.class, workflowOptions);
    /*
     * Execute our workflow and wait for it to complete. The call to our getGreeting method is
     * synchronous.
     */
    String greeting = workflow.getGreeting("World");
    // Display workflow execution results
    System.out.println(greeting);
    System.exit(0);
}
Also used : WorkflowClient(io.temporal.client.WorkflowClient) Worker(io.temporal.worker.Worker) WorkflowOptions(io.temporal.client.WorkflowOptions) WorkerFactory(io.temporal.worker.WorkerFactory) WorkflowServiceStubs(io.temporal.serviceclient.WorkflowServiceStubs)

Aggregations

WorkflowServiceStubs (io.temporal.serviceclient.WorkflowServiceStubs)49 WorkflowClient (io.temporal.client.WorkflowClient)41 WorkerFactory (io.temporal.worker.WorkerFactory)33 Worker (io.temporal.worker.Worker)32 WorkflowOptions (io.temporal.client.WorkflowOptions)15 TestWorkflowService (io.temporal.internal.testservice.TestWorkflowService)5 Test (org.junit.Test)5 Scope (com.uber.m3.tally.Scope)4 WorkflowExecution (io.temporal.api.common.v1.WorkflowExecution)4 PollWorkflowTaskQueueResponse (io.temporal.api.workflowservice.v1.PollWorkflowTaskQueueResponse)4 WorkflowStub (io.temporal.client.WorkflowStub)4 NoopScope (com.uber.m3.tally.NoopScope)3 StatusRuntimeException (io.grpc.StatusRuntimeException)3 WorkflowExecutionAlreadyStarted (io.temporal.client.WorkflowExecutionAlreadyStarted)3 WorkflowServiceStubsOptions (io.temporal.serviceclient.WorkflowServiceStubsOptions)3 HttpServer (com.sun.net.httpserver.HttpServer)2 RootScopeBuilder (com.uber.m3.tally.RootScopeBuilder)2 PrometheusMeterRegistry (io.micrometer.prometheus.PrometheusMeterRegistry)2 WorkflowException (io.temporal.client.WorkflowException)2 MicrometerClientStatsReporter (io.temporal.common.reporter.MicrometerClientStatsReporter)2