Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Ravi Bhaj
how to call serveResource() from liferay scheduler?
October 31, 2012 4:51 AM
Answer

Ravi Bhaj

Rank: New Member

Posts: 10

Join Date: September 27, 2012

Recent Posts

I have a usecase "There are 10 portlets, each has a serveResource() implementation to produce specific data. Create a liferay scheduler which runs and invoke these serveResource() to read the data. Such read data is E-mail'd to stakeholders."

I am new to Liferay and I tried few approaches.

1. Wrote a Java HttpClient to call the serveResource URL (hardcoded for development) and Later scheduler will call the Java HTTP Client. When I copy/paste the serveResource URL directly in a browser, I can see a download box coming (when I have already logged in. Otherwise ask for login and later I see the download box). With this approach I faced authentication issues and the serveResource URL gets redirected. When copy pasted the redirected URL it takes me to dashboard page (Again if I have logged in session otherwise will ask for a login). I wasn't able to hit and read the data from serveResourceURL.

2. Scheduler job directly calling the serverResource URL. Scheduler Job being a Java class, I wanted to programatically call serveResource URL, read the data and later send email. I think this is not possible as we can not programmatically call serveResource URL/method from java class. Also client programm will not have access to ResourceRequest and ResourceResponse objects to call serveResource()

3. Wrote a REST API to call serveResource(). And later the REST API can be called from a scheduler job. The serveResource() has a return type void and I write some data to ResourceResponse object. For this I have created a Portlet Project and my portlet class looks like this.....

@Path("/RestPortlet")
public class RestPortlet extends MVCPortlet {

@GET
@Override
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {

resourceResponse.setContentType("text/html");

try {
OutputStream out = resourceResponse.getPortletOutputStream();
byte[] bytes = new byte[1024];
bytes = "hello World".getBytes("UTF-8");

out.write(bytes);
out.flush();
out.close();
} catch(Exception e){
e.printStackTrace();
}
}

}

I have placed all the necessary jars in the lib and tried calling the serveResource() using the path. http://localhost:8080/Rest-portlet/resource/RestPortlet
I get below exception. As of now tried calling the above URL from browser itself. I am not sure how the ResourceResponse read from browser/client consuming REST API.

HTTP Status 415 - Unsupported Media Type
--------------------------------------------------------------------------------
type Status report
message Unsupported Media Type
description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Unsupported Media Type).

Dear Experts, please let me know if there is any better approaches to solve the usecase. Please do help with some code example.

Thanks very much in advance.

Ravindra
David H Nebinger
RE: how to call serveResource() from liferay scheduler?
October 31, 2012 5:29 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7250

Join Date: September 1, 2006

Recent Posts

Liferay portlets already can add scheduled calls. Check the liferay-portlet.xml dtd for how to define a scheduled task.

Your scheduled task should invoke the same logic as serveResource() would invoke.
Ravi Bhaj
RE: how to call serveResource() from liferay scheduler?
October 31, 2012 6:14 AM
Answer

Ravi Bhaj

Rank: New Member

Posts: 10

Join Date: September 27, 2012

Recent Posts

Thanks David for the quick response. That sounds intereting!

To be precise on my use case, I want to create a protlet template whose serveResource() is callable from a scheduler. The serveResource() of different portlets have their own logic and subsystems to generate the data. There should not be tight coupling between scheduler and the portlet. Based on some request/trigger, scheduler will call the serveResource().

I have created a sample scheduler like this.........

liferay-portlet.xml
---------------------
<scheduler-entry>
<scheduler-event-listener-class>com.csv.Listner.CsvJobListner</scheduler-event-listener-class>
<trigger>
<simple>
<simple-trigger-value>1</simple-trigger-value>
<time-unit>minute</time-unit>
</simple>
</trigger>
</scheduler-entry>

Listner
-------

public class CsvJobListner implements MessageListener {

@Override
public void receive(Message arg0) {

//Planning to place a logic to call serveResource of individual portlet based on some requirement. Read the content and email the same.
//Also wanted to take the triggering mechanism from liferay-portlet.xml
}



---> Is this a right way of programming the scheduler? I heard some quartz scheduler also which is supported by liferay.
---> The above use case can be designed something like this?

Thanks very much in advance.

Warm Regards,
Ravindra
Hitesh Methani
RE: how to call serveResource() from liferay scheduler?
October 31, 2012 7:05 AM
Answer

Hitesh Methani

Rank: Regular Member

Posts: 169

Join Date: June 24, 2010

Recent Posts

Hello Ravi,

For point 1, The scheduler defined in liferay-portlet.xml uses Quartz scheduler internally.
For point 2, yes you can place the code from serveresource into receive method of the listener, this is how schedulers are designed.

Thanks and Regards,
Hitesh
Ravi Bhaj
RE: how to call serveResource() from liferay scheduler?
November 1, 2012 1:01 AM
Answer

Ravi Bhaj

Rank: New Member

Posts: 10

Join Date: September 27, 2012

Recent Posts

Thanks Hitesh,

I am new to Liferay please forgive me for silly questions :-)

Looking at design perspective I cannot place the logic of serveResource logic in receive(). As I have many portlet with their own serveResource logic. So it is not feasible to have one receiver for each portlet. Instead I can have one receive() and multiple portlets. I should be in a position to call each serveResource based on some external trigger.

To be precise on the requirement let me summarize like this.....

1. Is it possible to call the serveResource() from a java class and read the data? I see that this method is called from the portlet container with valid ResourceRequest and ResourceResponse objects as parameter. Can we simulate this call from a java class? The call can be URL based or message based (object to object)
class A {
Public void someMethod(){
//Call serveResource() read data
//do some post processing
}
}

public class ExtPortlet extends MVCPortlet {

public void serverResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {
//Produce data
}

}


2. Is it possible to instantiate the portlet class and call a method in it? I mean call a method in a portlet class without any request/form action associated with it?