Play Framework – Writing Plugins

In the connection of the Play runtime, a plugin is a class that can connect to the Play life cycle, furthermore permits offering segments in a non static manner in your application.

Not any library that adds usefulness to Play – a class that give a custom channel to sample does not have to be a plug-in.

Likewise, plugins do not essentially intimate that they are re-utilize between applications, it is regularly extremely valuable to execute a plugin mainly inside an application, to guide into the Play lifecycle and offer parts in your code.

Implementing Plugins

Implementing a plugin obliges two steps. The principal is to execute the play.plugin interface. Executions of this interface must acknowledge a solitary contention of sort play.application:

package plugins;
import play.plugin;
import play.application;
public class Myplugin extends Plugin {
private last mycomponent = new Mycomponent();
public Myplugin(application application) {
}

public void onstart() {
mycomponent.start();
}

public void onstop() {
mycomponent.stop();
}

public boolean empowered() {
return genuine;
}

public Mycomponent getmycomponent() {
return mycomponent;
}
}

The following step is to enroll this with Play. This is possible by making a record called play.plugins and setting it in the foundation of the classloader. In a commonplace Play application, this implies placing it in the conf organizer:

2000:plugins.myplugin

Each one line in the play.plugins record contains a number emulated by the completely qualified name of the plugin to load. The number is utilized to control lifecycle requesting, lower numbers will be begun first and ceased last. Numerous plugins could be proclaimed in the one document, and any lines began with # are dealt with as remarks.

Picking the right number for requesting for a plugin is critical, it needs to fit in suitable as indicated by what different plugins it relies on upon. The plugins that Play uses utilize the accompanying requesting numbers:

  • 100 – Utilities that have no conditions, for example, the messages plugin
  • 200 – Database association pools
  • 300-500 – Plugins that rely on upon the database, for example, JPA, ebean and developments
  • 600 – The Play store plugin
  • 700 – The WS plugin
  • 1000 – The Akka plugin
  • 10000 – The Global plugin, which summons the Global.onstart and Global.onstop techniques. This plugin is planned to execute last.

Performing sample

A typical use case for utilizing plugins is to make and offer performing artists around the application. This is possible by actualizing a performer’s plugin:

package performing artists;
import akka.actor.*;
import play.*;
import play.libs.akka;

public class Actors extends Plugin {
private last application;
private Actorref myactor;

public Actors(application application) {
this.app = application;
}

public void onstart() {
myactor = Akka.system().actorof(myactor.props(), "my-performing artist");
}

public static Actorref getmyactor() {
return Play.application().plugin(actors.class).myactor;
}
}

Markdown

All markdown records must have interesting names over the whole documentation, paying little heed to what organizers they are in. Play utilizes a wiki style of interfacing and rendering documentation.

Newline characters amidst sections are viewed as hard wraps, like Gehub seasoned markdown, and are rendered as line breaks. Passages should hence be contained on a solitary line.

Links

Connections to different pages in the documentation should be made utilizing wiki markup language structure –

[[optional description|scalarouting]]

Outer connections should not utilize the above linguistic structure, yet rather, should utilize the standard Markdown join grammar.Pictures should likewise utilize the above linguistic structure.

Code tests

All underpinned code examples should be foreign made from outside assembled documents. The language structure for doing this is:

@[some-label](code/Somefeature.scala)</td>

The record should then delimit the lines that need to be concentrated utilizing #some-mark, for instance:

object Somefeaturespec develops Specification {
"some gimmick" should {
"do something" in {
//#some-mark
msg must_== "Keep Smiling"
}
}
}