import javax.inject.*;
import play.libs.ws.*;
public class MyComponent {
@Inject static WSClient ws;
@Inject static CacheApi cache;
@Inject static Play.api.Configuration config;
@Inject static Play.api.Environment env;
public static callSomething() {
if (ws == null) {
System.out.println("I have no ws!");
}
if (cache == null) {
System.out.println("I have no cache!");
}
if (config == null) {
System.out.println("I have no config!");
}
if (env == null) {
System.out.println("I have no env");
}
}
}--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/60ca2cf4-ac4a-4ee8-82fc-16f074adcd3a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
public class State {
public static List<String> getStates() {
return cache.getOrElse(...., ....);
}
}To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/456afe5d-bb3b-4942-81f9-45f97e4ec263%40googlegroups.com.
WSClient ws = Play.current().injector().instanceOf(WSClient.class);
CacheApi cache = Play.current().injector().instanceOf(CacheApi.class);I was asking the above question because I have a system that did not use the DI pattern. So if we do migration we will have to add classes such as StateRepository like you mentioned to introduce DI into the existing system.
To make it easier on transitioning to 2.5 would you guys consider adding an alternative to @Inject in the tutorial in addition to pushing towards using @Inject.
Also the next big change, that is a bit of problem for people who were using WSClient's F.Promise.get(TIME_OUT) as blocking call in conjunction with Cache.getOrElse() using Callable interface implementation.It seemed that the blocking call similar to F.Promise.get(TIME_OUT) is not there in CompletionStage<>.
Would be nice if any future version of play preserve the functionality of older APIs in someway so users don't have to drastically change their design when trying to upgrade.
public class AppSettingManagement {
private static final int CACHE_EXPIRATION = 2 * 60 * 60; // 2 hours
private static final int TIMEOUT = 20000;
public static AppSettings getAppSetting(String appName) throws Exception {
CacheApi cache = Play.current().injector().instanceOf(CacheApi.class);
return cache.getOrElse(CacheNames.APP_SETTINGS + "_" + appName, new AppSettingsRetriever(appName),
CACHE_EXPIRATION); // expires in 2 hours
}
static class AppSettingsRetriever implements Callable<AppSettings> {
private String appName;
public AppSettingsRetriever(String appName) {
this.appName = appName;
}
@Override
public AppSettings call() throws Exception {
WebRequest request = new WebRequest();
request.put("appName", appName);
.... // add other credential stuffs
WSClient ws = Play.current().injector().instanceOf(WSClient.class);
WSRequest wsRequest = ws.url(ConfigSettings.getAppSettingsUrl())
.setHeader("Content-Type", "application/json")
.setRequestTimeout(TIMEOUT);
CompletableFuture<JsonNode> jsonPromise = (CompletableFuture<JsonNode>)wsRequest.post(Json.toJson(request))
.thenApply(WSResponse::asJson);
WebResponse response = Json.fromJson(jsonPromise.get(), WebResponse.class);
if (response.isSuccess()) {
return (AppSettings) response.getResult();
}
else {
throw new Exception(response.getMessage());
}
}
}
}