I take Akka 2.0 and want to use it inside my web application. For this i create Filter to run an ActorSystem on filter inits.
Filter loads, starts Akka kernel Bootable implementation.
Now i create an Actor (for user authentication) and want to send a message to it at other part of application outside of scope my akka filter. Is any way to do this (i see only making some object to hold system val)? Maybe my logic to use Akka in this way is wrong?
Thanks.
Use a ServletContextListener to create and destroy the ActorSystem and set it as an attribute on your ServletContext (make a small façade to get it and set it perhaps?) Then just obtain it from the ServletContext within your Filter.
Related
I have a Scala-Play 2.5.x application that is actually a "Services Container" meaning under some service specification the user can hot-deploy services and they can be executed etc. Some of the services need common functionality like e.g. connection to a time-series database e.g. KDB.
I'd like to define in my Play application components that will know when the application starts and ends so they can do initialization and shutdown of their resources. I also need their "state" for example KDB connections to be made accessible to the deployed services in a seamless way i.e.
A request to execute a service is intercepted
The service "requirements" are inspected e.g. service X needs access to specific KDB connections
The service is generically instantiated and executed injecting the KDB connection required using IoC.
I have everything described above except this concept of "Component". I checked out Play books that talk about a previous play.api.Plugin trait that was a good start i.e. it had onStart onEnd etc. but it seems that this interface is gone in 2.5.x. Then I checked out how to create application Modules but all I see is an subclass of Module that overrides def bindings that does a bunch of IoC but nothing else. How can I cover my use-case of an application Component that has access to play web application lifecycle events and that can provide some state to specific requests?
From the manual: ScalaDependencyInjection could this help:
import scala.concurrent.Future
import javax.inject._
import play.api.inject.ApplicationLifecycle
#Singleton
class MessageQueueConnection #Inject() (lifecycle: ApplicationLifecycle) {
val connection = connectToMessageQueue()
lifecycle.addStopHook { () =>
Future.successful(connection.stop())
}
//...
}
This is the way for Play 2.5 and 2.6.
I am reading that the playframework is removing global state that is in older 2.4.x versions.
Can someone explain where the global state currently is and what are the benefits of removing global state?
What is the global state?
There is an object play.api.Play with the following field:
#volatile private[play] var _currentApp: Application = _
Where is it used?
Whenever you do Play.current you refer to that single mutable global field. This is used all across the framework to access things such as:
play.api.Play.configuration takes an implicit app
play.api.libs.concurrent.Execution.defaultContext calls the internal context, which uses the currently running app to get the actor system
play.api.libs.concurrent.Akka.system takes an implicit app
play.api.libs.ws.WS.url takes an implicit app
and many more places..
Why is that bad?
A number of functions just take an implicit app, so that's not really global state, right? I mean, you could just pass in that app. However where do you get it from? Typically, people would import play.api.Play.current.
Another example: Let's say you want to develop a component that calls a webservice. What are the dependencies of such a class? The WSClient. Now if you want to get an instance of that, you need to call play.api.libs.ws.WS.client and pass in an application. So your pretty little component that logically only relies on a webservice client, now relies on the entire application.
Another big factor and a direct consequence of the previous point is testing. Let's say you want to test your webservice component. In theory, you'd only need to mock (or provide some dummy implementation of) the webservice client. However now that somewhere in your code you're calling play.api.Play.current, you need to make sure that that field is set at the time it is called. And the easiest way to ensure that is to start the play application. So you're starting an entire application just to test your little component.
I want to have a custom setup method like for a test where i talk to a webservice and get initial values. Can i do this in gatling?I only want to run this once per simulation.
You can use the before hook to perform any custom code before running the simulation. Note that you can't use Gatling's DSL in there. But you can use whatever Java HTTP client you want, from java.net.UrlConnection to AsyncHttpClient that Gatling ships.
i have a little problem. I have to create a scala application. This application have to do some stuff. Create threads and other stuff. That isnt important for this question.
But to interact with this application it is neccessary to create a webinterface. I would like to create this interface by play.
The problem is, i have to integrate the play application inside my normal application. As component. And in the same moment as the application is runnig, he has to start also the play component. And if this worker is fail sometime, to restart it.
Is that possible? And if yes which way you would suggest.
You can invoke your normal application when play framework is starting. (http://www.playframework.com/documentation/2.2.x/ScalaGlobal) More specifically, onStart method in global object may be right place where you call your normal codes.
import play.api._
object Global extends GlobalSettings {
val yourInstance = new YourInstance()
override def onStart(app: Application) {
yourInstance.start()
}
}
Additionally, you can change your thread model to actor model. (http://www.playframework.com/documentation/2.2.x/ScalaAkka)
I'd like to observe a log file which is continuously updated by the system. I thought about using a Comet actor to do that but I need advice for the right direction. I want to achieve similar functionality to tail -F for the WebApp. If something new is written to the log file the Comet actor should pick it up continually. I want to show this information on a webpage. How can I achieve this with the Lift framework and Scala?
You need to solve 2 problems separately: log tailing, and comet page updates and then put everything together:
The way I would do it is by creating an Akka/Scala/Lift actor that tails the log. Look at this question for example on how to tail a log file in Java.
Then whenever this actor detects some changes in the log it should send a message to the comet actor which in turn will update the web page. You can easily find general examples on how to use Comet, for example here.
If you don't want to use an actor then just schedule a thread that runs and tails the log.
Note that if your log rotates and/or archives you'll have to deal with it too.
A completely alternative solution would be to hook up custom log appender or custom logger, for example this and make it send you log events. I think if you use Akka Logger you can simply subscribe to the event which your actor will receive on each log event, which you just forward to the Comet actor in a similar manner to the solution above.