You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by alexis <al...@gmail.com> on 2011/02/01 01:27:28 UTC

Re: custom class as resource

sure.

This class is the one that starts all threads for processing (this one is started from a Listener)

---------------------------
public class LCMS3 implements ServletContextListener {

    private DAO dao = null;
    private org.apache.log4j.Logger log = Logger.getLogger(LCMS3.class);
    private Thread schedT;
    private Thread queueT;
    private TelnetClient[] tcs;
    private Properties p;
    private static final String ____s = "31d0a1b4b2ecaf7ca14f5baf8ff44cdf";
    private License lic = null;
    private Calendar licenseValidTo;


    public void contextInitialized(ServletContextEvent sce) {

        log.info("Starting LCMS3 daemon");

        ServletContext sc = sce.getServletContext();
        LiveInfo li = new LiveInfo();
        li.setTest("test99test99");
        sc.setAttribute("live", li);
....
---------------------------------

Then, i have a class that is the responsible to start n threads that sends commands to the external device, receives the command and call another class to parse the result, it is

---------------------------------

public class Queue  implements Runnable {

    private TelnetClient[] tcs;
    private DAO dao;
    private Calendar licenseValidTo;
    private boolean exec = false;
    private static org.apache.log4j.Logger log = Logger.getLogger(Queue.class);

    public Queue(TelnetClient[] value, Calendar calValue) {
        this.tcs = value;
        this.licenseValidTo = calValue;

        Calendar now = Calendar.getInstance();

        if (now.compareTo(licenseValidTo) < 0) {
            exec = true;
        }

        if (dao == null) {
            dao = new DAO();
        }
    }

    @Override
    public void run() {
        if (exec) {
            try {

                NewClass nc = new NewClass();
                log.debug(">>>>>>>>>>>>>>"+nc.aVer());
                
                Thread[] queues = new Thread[tcs.length];

                //initialize threads
                for (int i = 0; i < tcs.length; i++) {
                    queues[i] = new Thread((Runnable) new QueueExec(tcs[i], new Command()));
                }

                while (true) {
                    Thread.sleep(1500L);
                    Command[] c = dao.getCommand();

                    if (c.length > 0) {
                        for (Command xC : c) {
                            for (int i = 0; i < queues.length; i++) {
                                if (!queues[i].isAlive()) {
                                    queues[i] = new Thread((Runnable) new QueueExec(tcs[i], xC));
                                    queues[i].start();
                                    //wait execution for 5 seconds then discard the command
                                    queues[i].join(20000);
                                    queues[i].interrupt();
                                    break; //command is being processed by this thread
                                } else {
                                    queues[i].join(20000);
                                    queues[i].interrupt();
                                }
                            }
                        }
                    }
                }
            } catch (InterruptedException e) {
                log.error("Queue Manager Interruption request ... exiting");
                dao.closeConnection();
                dao = null;
                return;
            }
        } else {
            log.error("Wont exec queue ... license expired");
        }
    }

    class QueueExec implements Runnable {

        private ArrayList<String[]> fields = new ArrayList<String[]>();
        private TelnetClient tc;
        private Command c;
        private String queueId;

        QueueExec(TelnetClient tcValue, Command cValue) {
            this.tc = tcValue;
            this.c = cValue;
            this.queueId = tc.getLocalPort() + ">" + tc.getRemotePort();
            String logLine = "Starting queue with connection: " + queueId;
            if (c.getCommand() != null) {
                logLine += " command >>" + c.getCommand() + "<<";
            }

            log.debug(logLine);
        }

        public void run() {
            Telnet t = new Telnet();
            try {
                fields = Parser.parse(t.sendCommand(this.tc, c.getCommand()));
                Parser.processResult(c.getCommand(), fields);
            } catch (SQLException ex) {
                log.error("", ex);
            } catch (LCMS3Exception ex) {
                log.error("", ex);
            }
        }
    }
}
---------------------------------

Then, i have the Parser class that  is

public class Parser   {

    private  static org.apache.log4j.Logger log = Logger.getLogger(Parser.class);
    private  static DAO dao = null;


    public static ArrayList<String[]> parse(String result) throws LCMS3Exception {
        ArrayList<String[]> parsed = new ArrayList<String[]>();
        ArrayList<Integer> skel = new ArrayList<Integer>();
....

---------------------------------

So, LCMS3 starts a thread Queue, and Queue when there's commands to execute starts QueueExec and sends/receive/process the commands.

ProcessResults on Parser class, stores results on DB and that works ok.


Then i made a simple class to test get the live attribute from the ServletContext that i've setted at the start of LCMS3, that is this one

---------------------------------


---------------------------------
public class testcontext extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            ServletContext sc = getServletContext();

            LiveInfo li = (LiveInfo) sc.getAttribute("live");

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet testcontext</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet testcontext at " + request.getContextPath () + "</h1>");
            out.println("<p>test: "+li.getTest()+"</p>");
            out.println("</body>");
            out.println("</html>");

        } finally { 
            out.close();
        }
    } 
-------------------------------

pointing my browser to my app (example) http://localhost:8080/lcms3/testcontext i can perfectly see test: test99test99

That is what i setted on my context at my app start.

Up to here, everything works without any problem.

But, what i need and i cannot achieve is that Parser class get "live" attribute from ServletContext, to store results on it, so, testcontext can see those results.





On Jan 31, 2011, at 7:54 PM, Christopher Schultz wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Alexis,
> 
> On 1/31/2011 5:23 PM, alexis wrote:
>> Sure, find attached 3 files
> 
> This list strips attachments.
> 
>> This app is intended to do the following (extremely [abridged])
> 
> How about a snip of the code that actually loads instantiates the
> servlet objects?
> 
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> 
> iEYEARECAAYFAk1HPaMACgkQ9CaO5/Lv0PAYLwCeOSXLCLj/xGmwoHt40QsiOU03
> XJcAni9odKM72OSRNQFuLzHe6jMrJXjC
> =dFEy
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 


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