[opencms-dev] Scheduled Job

Aitor de la Puente aitor at dicampus.com
Wed May 10 13:42:48 CEST 2006


Hi.

I did find the problem some days ago: I have to "load" the html file with a 
Stream.

--------------------------------
	public String launch(CmsObject arg0, Map arg1) throws Exception {
        
		if ( ! 
arg0.getRequestContext().currentProject().getName().equalsIgnoreCase("offline") )
			arg0.getRequestContext().setCurrentProject( arg0.readProject("Offline") );
        
        StringBuffer sb = null;
        CmsFile cmsFile = null;
        String nombreFichero = "/sites/default/mySite/tareas/resultado_" + 
(new Date()).getTime() + ".txt";  // log file
        
        URL gotoUrl = null;
        InputStreamReader isr = null;
        BufferedReader in = null;
        
        String inputLine = null;
        
        try { 
            
            sb = new StringBuffer();
            
            gotoUrl = new URL( (String)arg1.get("url1") );
            isr = new InputStreamReader( gotoUrl.openStream() ); 
            in = new BufferedReader( isr );

            sb.append( "url1 : " + (String)arg1.get("url1") + "\r\n" );
            //grab the contents at the URL 
            while ( (inputLine = in.readLine()) != null ) { 
                sb.append( inputLine + "\r\n" );
            }
            
        } catch (MalformedURLException mue) { 
            sb = new 
StringBuffer( mue.getMessage() ); //mue.printStackTrace(); 
        }
        
        arg0.copyResource( "/sites/default/mySite/tareas/resultado.txt", 
nombreFichero );
        cmsFile = arg0.readFile( nombreFichero );
        cmsFile.setContents( sb.toString().getBytes() );
        arg0.writeFile( cmsFile );
        
        return "ok";
    }
--------------------------------

El Lunes, 1 de Mayo de 2006 16:04, Aitor de la Puente escribió:
> Hello.
>
> I need to execute a file of my project in a scheduled job. This file is
> "/sites/default/myproject/myjobs/job.html". I have published it, and it
> works alright. It is neccesary to access to the file in the Offline mode.
> So I created a task:
>
> -----------------------------------------------------------------
> package com.myproject.myjobs;
>
> import java.util.List;
> import java.util.Map;
>
> import org.opencms.file.CmsObject;
> import org.opencms.file.collectors.I_CmsResourceCollector;
> import org.opencms.flex.CmsFlexCache;
> import org.opencms.flex.CmsFlexCacheConfiguration;
> import org.opencms.loader.CmsJspLoader;
> import org.opencms.main.OpenCms;
>
> public class Job1 implements org.opencms.scheduler.I_CmsScheduledJob {
>
> 	public String launch(CmsObject arg0, Map arg1) throws Exception {
>
> 		CmsJspLoader crl = new CmsJspLoader();
>
>         crl.setFlexCache( new CmsFlexCache( new
> CmsFlexCacheConfiguration() ) );
>
>         crl.initConfiguration();
>
>         I_CmsResourceCollector collector =
> OpenCms.getResourceManager().getContentCollector( "singleFile" );
>
>         List m_collectorResult = collector.getResults (
>         		arg0,
>         		"singleFile",
>         		"/sites/default/myproject/myjobs/job.html" );
> 			// also I have used "/myproject/myjobs/job.html"
>
>         return "ok";
>     }
>
> }
> -----------------------------------------------------------------
>
> And I created a scheduled job in the "Scheduled Jobs Management":
>
> Job settings.
> Job name: Job 1
> Java class: com.myproject.myjobs.Job1
> Cron expresion: 0 0/5 * * * ?
> Active: X
>
> Context info.
> User name: Admin
> Project: Offline
>
>
> This job is executed every 5 minutes, but it doesn't realize the changes
> that it have to do. Nevertheless, if I accede to the file from workplace it
> works fine.
>
> Where is the mistake?
>
> Thaks.

-- 

Saludos.

--
-- Aitor de la Puente
--
-- Dicampus  [ http://www.dicampus.com ]
-- Imaginando el futuro
--



More information about the opencms-dev mailing list