Try to search your question here, if you can't find : Ask Any Question Now ?

How to use / inject WSClient in akka actor in Playframework-2.5.x?

HomeCategory: stackoverflowHow to use / inject WSClient in akka actor in Playframework-2.5.x?
Avatarrupesh asked 5 months ago

I have to hit an API on dialy basis.
For that I have
1. created an actor, scheduler
2. bind actor & scheduler in Module
3. Enabled that Module in application.conf file

The scheduling is working fine, but now i want to inject WSClient into actor, send request using ws, record response into database.
I am getting issue While injecting WSClient into Actor.I have refer this:- Reference

What I have done is :-

package controllers;

import akka.actor.UntypedActor;
import play.Logger;
import javax.inject.Inject;

import play.mvc.*;
import play.libs.ws.*;
import java.util.concurrent.CompletionStage;

public class CronActor extends UntypedActor{

    @Override
    public void onReceive(final Object message) throws Throwable {
        Logger.info("Write your crone task or simply call your method here that perform your task"+message);

        @Inject WSClient ws;

        WSRequest request = ws.url("https://api.exchangeratesapi.io/latest?base=USD&symbols=SGD,INR");

        WSRequest complexRequest = request.setHeader("Accept", "Application/json");
        CompletionStage<WSResponse> responsePromise = complexRequest.get();
        //WSRequest complexRequest = request.setQueryParameter("base", "paramValue");
        Logger.info("response is"+responsePromise)

    }

}

I am getting following Error:-

 error: annotation type not applicable to this kind of declaration
[error]         @Inject WSClient ws;
[error]         ^
[error] (compile:compileIncremental) javac returned nonzero exit code

So, how I can inject WSClient into Actor, in Playframework-2.5.x?

1 Answers
Best Answer
AvatarMatthias answered 5 months ago
Your Answer

1 + 16 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com