[opencms-dev] how to use the Scheduled tasks?
fangzhu
chin at mediafusion.co.jp
Thu May 15 14:35:02 CEST 2003
hi there,
i've been tried to use the Scheduled Tasks these days. but it wasn't working
at all.
i opened the "Scheduled tasks" from Administration view, and enterd a line
like this:
30 21 15 5 * Admin Users Publish 4
then i wrote a file "Publish.java" like this:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.opencms.core.CmsException;
import com.opencms.core.I_CmsCronJob;
import com.opencms.file.CmsObject;
public class Publish extends HttpServlet implements I_CmsCronJob
{
ServletContext application = getServletConfig().getServletContext();
public String launch(CmsObject arg0, String arg1)
throws IOException, CmsException
{
Document document = null;
try
{
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
document =
builder.parse(
new File(
application.getRealPath("export/Sample/addbook.xml")));
Element element = document.createElement("new");
element.appendChild(
document.createTextNode("this is the new element"));
Node member = document.getElementsByTagName("member").item(0);
member.insertBefore(
element,
document.getElementsByTagName("address").item(0));
StringWriter sw = new StringWriter();
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new DOMSource(document), new StreamResult(sw));
String out = sw.toString().replaceFirst("UTF-8", "Shift_Jis");
//out print all the document to temp.xml
FileWriter fw =
new FileWriter(
new File(
application.getRealPath("export/Sample/addbook.xml")));
try
{
fw.write(out);
fw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
catch (Exception e)
{
e.printStackTrace();
}
int project_id = Integer.parseInt(arg1);
arg0.publishProject(project_id);
return "Scheduler launched successfully!";
}
public static void main(String[] args)
{
CmsObject cms = new CmsObject();
Publish publish = new Publish();
String string = "";
try
{
string = publish.launch(cms, "4");
}
catch (IOException e)
{
e.printStackTrace();
}
catch (CmsException e)
{
e.printStackTrace();
}
System.out.println(string);
}
}
the class simplely add a node to an xml file.
the problem is that i don't know how to define a CmsObject in main method
which is needed to call the launch mothod.
i complied it and put the class into C:\Program Files\Apache Tomcat
4.0\webapps\opencms\WEB-INF\classes\, but it didn't work.
could someone with experence of Scheduled tasks give me a simple example
java source?
and is there a API documentation of Opencms somethere?
thank you in advance!
With regards
---------------------------------
fangzhu from japan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20030515/1030dfa5/attachment.htm>
More information about the opencms-dev
mailing list