cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ard Schrijvers" <a.schrijv...@hippo.nl>
Subject RE: problems getting the BackgroundEnvironment working within a thread
Date Wed, 28 Dec 2005 12:24:49 GMT
Hi Marcel,

first of all, I notice a lot of people lately having more or less the same problem, including
myself. I think it is way to hard in cocoon to call pipelines which were initiated from some
background things. 

Anyway, think you should do something like:

Make your class Contextualizable (which you probably already did)


private org.apache.cocoon.environment.Context environmentContext;

public void contextualize(Context ctx) throws ContextException
    {
        this.environmentContext = (org.apache.cocoon.environment.Context) ctx.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);

    }

You will need 

import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;

and 

import org.apache.cocoon.Constants;


Now, when you want the SourceResolver, do someting like:

	      BackgroundEnvironment env = null;
            Processor processor = null;
            Object key = null;

            try
            {
                env = new BackgroundEnvironment(getLogger(), environmentContext);
                
                try {
                    processor = (Processor) manager.lookup(Processor.ROLE);
                } catch (ServiceException e) {
                    throw new CascadingRuntimeException("Couldn't look up the Processor",e);
                }

                env.startingProcessing();
                
                key = CocoonComponentManager.startProcessing(env);
                CocoonComponentManager.enterEnvironment(env, new WrapperComponentManager(manager),
processor);
 
                SourceResolver resolver = null;
                try
                {
                     
                    resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
  
                    Source inputSource = resolver.resolveURI("cocoon://foo");
                    InputStream sourceStream = inputSource.getInputStream();
                    try
                    {
				// example what you could do with the inputStream 
                        DefaultHandler doHandler = new DefaultHandler();
                        SourceUtil.toSAX(inputSource, doHandler);
                                               
                    }
                    catch (ProcessingException e)
                    {
                        e.printStackTrace();
                    }
                    finally
                    {
                        sourceStream.close();
                    }
                }
                catch (ServiceException e2)
                {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
                catch (MalformedURLException e1)
                {
                    e1.printStackTrace();
                }
                catch (IOException e1)
                {
                    e1.printStackTrace();
                }
                catch (SAXException e)
                {
                    e.printStackTrace();
                }
                finally
                {
                    if(resolver!=null)
                        manager.release(resolver);
                }
            

            }
            catch (MalformedURLException e)
            {

                e.printStackTrace();
            }
            finally
            {
                CocoonComponentManager.leaveEnvironment();
                CocoonComponentManager.endProcessing(env, key);
                if (processor != null) {
                    manager.release(processor);
                }
            }

Hope you can extract the parts you need,

AS



-----Original Message-----
From: Marcel Rouwenhorst [mailto:marcel.rouwenhorst@mobillion.nl]
Posted At: zaterdag 24 december 2005 11:11
Posted To: Cocoon User List
Conversation: problems getting the BackgroundEnvironment working within a thread
Subject: problems getting the BackgroundEnvironment working within a thread


Hi,
 
I'm currently working on a mail daemon that sends mail from an oracle queue. (I don't want
to use the cron block because it should be a continues thread in de background). I created
a new daemon thread with the RunnableManager. This still works fine but when I want to call
a pipeline with the SourceResolver from within the run method I get an exception that the
cocoon protocol can't be used. 
 
I understood from some other mail in this mailing list that I should make a 'fake' environment
with the BackgroundEnvironment. I'm trying to create a new instance of this class but that
fails because I need a context. If I implement Contextualizable I get a Context - an Avalon
context - but this one is incompatible with context - a Cocoon environment context - that
the BackgroundEnvironment needs. 
 
What am I doing wrong here? Any help is appreciated!
 
Thanks,
Marcel
 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message