Possible to integrate existing Java class with Actor?
I am on Scala 2.10.2, Akka 2.2.0 and trying to determine if it is
meaningful to integrate an existing Java class with an Actor. The Java
class I have in mind is the Apache Commons FileUpload streaming API.
This is what I got working without using an Actor
class HelloWorld extends HttpServlet {
override def doGet(req: HttpServletRequest, resp: HttpServletResponse) = {
resp.getWriter().print("Hello World!")
}
override def doPost(req: HttpServletRequest, resp: HttpServletResponse )
= {
if (ServletFileUpload.isMultipartContent(req)) {
val upload = new ServletFileUpload()
// Parse the request
val iter = upload.getItemIterator(req)
while (iter.hasNext()) {
val item = iter.next()
val name = item.getFieldName()
val stream = item.openStream()
if (item.isFormField()) {
println("Form field " + name + " with value " +
Streams.asString(stream) + " detected.")
} else {
println("File field " + name + " with file name " +
item.getName() + " detected.")
saveAttachment(item.getName(), stream)
}
}
resp.getWriter().print("\nFile uploads success!\n")
} else
resp.getWriter().print("\nNo file upload found!\n")
}
private def saveAttachment(...) {...}
}
Typically an actor handles all its messages within the receive method but
in this case the class already has predefined methods. For lack of a
better term, is there a way to actorfy this?
No comments:
Post a Comment