Using Spray and Akka to Connect to CouchDB

109 views
Skip to first unread message

Nweike Onwuyali

unread,
Apr 16, 2015, 4:07:05 AM4/16/15
to akka...@googlegroups.com
Hello All,
I am a newbie to the Typesafe framework so i might be asking a naive question.
I am building a new application using Play and CouchDB as my database. I want to make my connection to the CouchDB a non-blocking operation. Also i want to connect to CouchDB using the JSON API. 

I will prefer not to use spray-http Marshalling and Unmarshalling support. Will it be appropriate to have Actors receive raw  HttpRequest and send message to CouchDB and receive a response using HttpResponse?.

I need a guide on how to architect the system.


Patrik Nordwall

unread,
Apr 20, 2015, 6:21:41 AM4/20/15
to akka...@googlegroups.com
If the CouchDB API is HTTP based you can use an async http client, such as the Spray HTTP client, Akka HTTP client or Play WS API, and convert the responses to actor messages if you want to process the result in actors. Perhaps I misunderstand the question completely?

/Patrik

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--

Patrik Nordwall
Typesafe Reactive apps on the JVM
Twitter: @patriknw

Nweike Onwuyali

unread,
Apr 20, 2015, 4:48:23 PM4/20/15
to akka...@googlegroups.com
Hi,
Thanks for your response. I want to query a couchDB using REST. However, i want to put the business logic of doing so in Actors(Akka) instead of Play models. The Actors will perform the request and also receive the response from the database. The Actors can then forward the results to Play Controller.

How can i achieve this?

Patrik Nordwall

unread,
Apr 24, 2015, 11:01:12 AM4/24/15
to akka...@googlegroups.com
On Mon, Apr 20, 2015 at 10:38 PM, Nweike Onwuyali <nweikeo...@gmail.com> wrote:
Hi,
Thanks for your response. I want to query a couchDB using REST. However, i want to put the business logic of doing so in Actors(Akka) instead of Play models. The Actors will perform the request and also receive the response from the database. The Actors can then forward the results to Play Controller.

How can i achieve this?

I'm afraid your question is too broad for me to be able to give any advice on this.

One nice activator template that is using Play and backend actors is Reactive Maps.

/Patrik

Nweike Onwuyali

unread,
Apr 27, 2015, 1:01:26 AM4/27/15
to akka...@googlegroups.com
Hi Patrick,
Thanks for your response. Let me explain what i want to achieve.
1) When a user performs registration, i want to connect to couchDB and persist the data
2)I want to send the user an email confirming the registration

This is what i have implemented.

a) I am using Play WS API to connect to couchDB to persist data using the following code:
   <xmp>
public static Promise<Result> create() throws Exception{

Promise<Result> resultPromise=null;
try {
User user = getRegisteredUser();

DataRequestObject dataRequestObject= new DataRequestObject(new HashMap(), RequestMethod.POST,getClientIpAddr());

Promise<WSResponse> responsePromise = user.save(UserStep.REGISTRATION, DataRepositoryStrategy.CLOUDANT,WS.client(),dataRequestObject);

resultPromise = responsePromise.map(
new Function<WSResponse, Result>() {
@Override
public Result apply(WSResponse response) throws Exception {
ResponseObject responseObject = DBCodesFactory.getResponseObject(response.asJson(), new String[]{"email", user.name.username});
if (responseObject.isSuccess()) {

return ok(views.html.postreg.render());
} else {
flash("error", responseObject.getFailureReason());
return redirect(routes.Users.register());// ok(views.html.register.render());

}
}
});

}catch (Exception d){
Logger.getLogger("NEW REGISTRATION").log(Level.SEVERE, d.toString(), d);
d.printStackTrace();
throw new Exception(d);
}
return resultPromise;



}
</xmp>
b) This is my challenge. I want to send an email  when responseObject.isSuccess().

How can i implement this using Actors?

Patrik Nordwall

unread,
Apr 27, 2015, 3:38:02 AM4/27/15
to akka...@googlegroups.com
On Thursday, April 16, 2015 at 9:07:05 AM UTC+1, Nweike Onwuyali wrote:
Hello All,
I am a newbie to the Typesafe framework so i might be asking a naive question.
I am building a new application using Play and CouchDB as my database. I want to make my connection to the CouchDB a non-blocking operation. Also i want to connect to CouchDB using the JSON API. 

I will prefer not to use spray-http Marshalling and Unmarshalling support. Will it be appropriate to have Actors receive raw  HttpRequest and send message to CouchDB and receive a response using HttpResponse?.

I need a guide on how to architect the system.


--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages