Skip to content

Instantly share code, notes, and snippets.

@harry0000
Last active November 10, 2016 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harry0000/13df90b60c20814fc8bc8661f9bcba43 to your computer and use it in GitHub Desktop.
Save harry0000/13df90b60c20814fc8bc8661f9bcba43 to your computer and use it in GitHub Desktop.
import com.google.inject.AbstractModule;
import java.time.Clock;
import services.ApplicationTimer;
import services.AtomicCounter;
import services.Counter;
import services.MyApplicationGlobal;
/**
* This class is a Guice module that tells Guice how to bind several
* different types. This Guice module is created when the Play
* application starts.
*
* Play will automatically use any class called `Module` that is in
* the root package. You can create modules in other locations by
* adding `play.modules.enabled` settings to the `application.conf`
* configuration file.
*/
public class Module extends AbstractModule {
@Override
public void configure() {
// Use the system clock as the default implementation of Clock
bind(Clock.class).toInstance(Clock.systemDefaultZone());
// Ask Guice to create an instance of ApplicationTimer when the
// application starts.
bind(ApplicationTimer.class).asEagerSingleton();
// Set AtomicCounter as the implementation for Counter.
bind(Counter.class).to(AtomicCounter.class);
bind(MyApplicationGlobal.class).asEagerSingleton();
}
}
package services;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Singleton;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.UntypedActor;
import play.Logger;
import play.inject.ApplicationLifecycle;
import scala.concurrent.duration.Duration;
@Singleton
public class MyApplicationGlobal {
@Inject
public MyApplicationGlobal(final ActorSystem system, ApplicationLifecycle lifecycle) {
// サーバ起動時の動作
Logger.info("----------------------------------- Start application... ------------------------------");
ActorRef actor = system.actorOf(
akka.actor.Props.create(HelloActor.class, "world")
);
system.scheduler().schedule(
Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay 0 milliseconds
Duration.create(30, TimeUnit.MINUTES), //Frequency 30 minutes
actor,
"tick",
system.dispatcher(),
null
);
lifecycle.addStopHook(() -> {
Logger.info("----------------------------------- Stop application... ------------------------------");
return CompletableFuture.completedFuture(null);
});
}
}
class HelloActor extends UntypedActor {
private final String name;
public HelloActor(String name) {
this.name = name;
}
@Override
public void onReceive(Object arg) throws Exception {
System.out.println(java.text.MessageFormat.format("Hello, {0}!", name));
}
}
name := """play-java"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayJava)
scalaVersion := "2.11.8"
libraryDependencies += javaJdbc
libraryDependencies += cache
libraryDependencies += javaWs
#Activator-generated Properties
#Tue Oct 04 16:51:22 CDT 2016
template.uuid=26c759a5-daf0-4e02-bcfa-ac69725267c0
sbt.version=0.13.13
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.9")
// Web plugins
addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.1.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.4")
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.8")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.1")
addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0")
addSbtPlugin("org.irundaia.sbt" % "sbt-sassify" % "1.4.6")
// Play enhancer - this automatically generates getters/setters for public fields
// and rewrites accessors of these fields to use the getters/setters. Remove this
// plugin if you prefer not to have this feature, or disable on a per project
// basis using disablePlugins(PlayEnhancer) in your build.sbt
addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment