All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.linkedin.restli.examples.greetings.server.MixedResource Maven / Gradle / Ivy

Go to download

Pegasus is a framework for building robust, scalable service architectures using dynamic discovery and simple asychronous type-checked REST + JSON APIs.

There is a newer version: 27.7.18
Show newest version
/*
   Copyright (c) 2012 LinkedIn Corp.

   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

       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 com.linkedin.restli.examples.greetings.server;

import com.linkedin.common.callback.Callback;
import com.linkedin.parseq.promise.Promise;
import com.linkedin.parseq.promise.Promises;
import com.linkedin.parseq.promise.SettablePromise;
import com.linkedin.restli.common.HttpStatus;
import com.linkedin.restli.examples.greetings.api.Greeting;
import com.linkedin.restli.server.CreateResponse;
import com.linkedin.restli.server.UpdateResponse;
import com.linkedin.restli.server.annotations.Action;
import com.linkedin.restli.server.annotations.CallbackParam;
import com.linkedin.restli.server.annotations.Finder;
import com.linkedin.restli.server.annotations.QueryParam;
import com.linkedin.restli.server.annotations.RestLiCollection;
import com.linkedin.restli.server.annotations.RestMethod.Create;
import com.linkedin.restli.server.annotations.RestMethod.Delete;
import com.linkedin.restli.server.annotations.RestMethod.Get;
import com.linkedin.restli.server.annotations.RestMethod.Update;
import com.linkedin.restli.server.resources.KeyValueResource;
import com.linkedin.restli.server.resources.ResourceContextHolder;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * This resource demonstrates mixing of various method signatures: synchronous, callback,
 * promise
 *
 * @author jnwang
 */
@RestLiCollection(name = "mixed", namespace = "com.linkedin.restli.examples.greetings.client")
public class MixedResource extends ResourceContextHolder implements
    KeyValueResource
{
  private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
  private final static int                      DELAY     = 200;

  @Get
  public Greeting get(Long key)
  {
    return new Greeting().setMessage(key.toString());
  }

  @Create
  public void create(Greeting entity, @CallbackParam final Callback callback)
  {
    final Runnable requestHandler = new Runnable()
    {
      public void run()
      {
        callback.onSuccess(new CreateResponse(HttpStatus.S_200_OK));
      }
    };
    scheduler.schedule(requestHandler, DELAY, TimeUnit.MILLISECONDS);
  }

  @Update
  public Promise update(Long key, Greeting entity)
  {
    final SettablePromise result = Promises.settable();
    Runnable requestHandler = new Runnable()
    {
      public void run()
      {
        result.done(new UpdateResponse(HttpStatus.S_200_OK));
      }
    };
    scheduler.schedule(requestHandler, DELAY, TimeUnit.MILLISECONDS);
    return result;
  }

  @Delete
  public Promise delete(Long key)
  {
    return Promises.value(new UpdateResponse(HttpStatus.S_200_OK));
  }

  @Finder("search")
  public Promise> search(@QueryParam("what") final String s)
  {
    final SettablePromise> result = Promises.settable();
    Runnable requestHandler = new Runnable()
    {
      public void run()
      {
        result.done(Arrays.asList(new Greeting().setMessage(s)));
      }
    };
    scheduler.schedule(requestHandler, DELAY, TimeUnit.MILLISECONDS);
    return result;
  }

  @Action(name = "theAction")
  public void theAction(@CallbackParam Callback callback)
  {
    callback.onSuccess("theResult");
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy