view flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/App.java @ 5779:ebec12def170

Datacage: Add a pool of builders to make it multi threadable. XML DOM is not thread safe. Therefore the old implementation only allowed one thread to use the builder at a time. As the complexity of the configuration has increased over time this has become a bottleneck of the whole application because it took quiet some time to build a result. Furthermore the builder code path is visited very frequent. So many concurrent requests were piled up resulting in long waits for the users. To mitigate this problem a round robin pool of builders is used now. Each of the pooled builders has an independent copy of the XML template and can be run in parallel. The number of builders is determined by the system property 'flys.datacage.pool.size'. It defaults to 4.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 21 Apr 2013 12:48:09 +0200
parents 0a5eff5511b1
children
line wrap: on
line source
package de.intevation.flys.artifacts.datacage.templating;

import java.util.Map;
import java.util.HashMap;

import java.io.IOException;
import java.io.OutputStream;
import java.io.File;
import java.io.FileOutputStream;

import de.intevation.flys.backend.SessionFactoryProvider;

import org.hibernate.Session;

import org.w3c.dom.Document;

import org.apache.log4j.Logger;

import de.intevation.artifacts.common.utils.XMLUtils;

import de.intevation.flys.artifacts.datacage.Recommendations;

public class App
{
    private static Logger log = Logger.getLogger(App.class);

    public static final String template =
        System.getProperty("meta.data.template", "meta-data.xml");

    public static final String userId =
        System.getProperty("user.id");

    public static final String PARAMETERS =
        System.getProperty("meta.data.parameters", "");

    public static final String OUTPUT =
        System.getProperty("meta.data.output");

    public static Map<String, Object> getParameters() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        String [] parts = PARAMETERS.split("\\s*;\\s*");
        for (String part: parts) {
            String [] kv = part.split("\\s*:\\s*");
            if (kv.length < 2 || (kv[0] = kv[0].trim()).length() == 0) {
                continue;
            }
            String [] values = kv[1].split("\\s*,\\s*");
            map.put(kv[0], values.length == 1 ? values[0] : values);
        }
        return map;
    }

    public static void main(String [] args) {

        Recommendations rec = Recommendations.createRecommendations(
            new File(template));

        if (rec == null) {
            System.err.println("No recommendations created");
            return;
        }

        final Document result = XMLUtils.newDocument();

        final Map<String, Object> parameters = getParameters();

        Session session = SessionFactoryProvider
            .createSessionFactory()
            .openSession();

        try {
            rec.recommend(parameters, userId, result, session);
        }
        finally {
            session.close();
        }

        OutputStream out;

        if (OUTPUT == null) {
            out = System.out;
        }
        else {
            try {
                out = new FileOutputStream(OUTPUT);
            }
            catch (IOException ioe) {
                log.error(ioe);
                return;
            }
        }

        try {
            XMLUtils.toStream(result, out);
        }
        finally {
            if (OUTPUT != null) {
                try {
                    out.close();
                }
                catch (IOException ioe) {
                    log.error(ioe);
                }
            }
        }
        System.exit(0);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org