Project

General

Profile

Download (5.44 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2015 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.cdm.server.instance;
11

    
12
import java.util.HashSet;
13
import java.util.LinkedList;
14
import java.util.Set;
15

    
16
import org.apache.log4j.Logger;
17
import org.eclipse.jetty.util.component.LifeCycle;
18
import org.eclipse.jetty.util.component.LifeCycle.Listener;
19

    
20
/**
21
 * @author a.kohlbecker
22
 * @date Jul 29, 2015
23
 *
24
 */
25
public class StartupQueue extends LinkedList<CdmInstance> {
26

    
27
    private static final long serialVersionUID = -8173521573512154767L;
28

    
29
    private final Logger logger = Logger.getLogger(InstanceManager.class);
30

    
31
    Set<CdmInstance> instancesStartingUp = new HashSet<CdmInstance>();
32

    
33
    private int parallelStartUps = 1;
34

    
35
    /**
36
     * @return the parallelStartUps
37
     */
38
    public int getParallelStartUps() {
39
        return parallelStartUps;
40
    }
41

    
42
    /**
43
     * @param parallelStartUps
44
     *            the parallelStartUps to set
45
     */
46
    public void setParallelStartUps(int parallelStartUps) {
47
        this.parallelStartUps = parallelStartUps;
48
    }
49

    
50
    /**
51
     * {@inheritDoc}
52
     */
53
    @Override
54
    public boolean add(CdmInstance e) {
55
        boolean result = super.add(e);
56
        registerAt(e);
57
        return result;
58
    }
59

    
60
    /**
61
     * {@inheritDoc}
62
     */
63
    @Override
64
    public void addFirst(CdmInstance e) {
65
        super.addFirst(e);
66
        registerAt(e);
67
    }
68

    
69
    /**
70
     * {@inheritDoc}
71
     */
72
    @Override
73
    public void addLast(CdmInstance e) {
74
        super.addLast(e);
75
        registerAt(e);
76
    }
77

    
78
    protected void notifyInstanceStartedUp(CdmInstance instance) {
79
        logger.debug("received message that instance " + instance.getName() + " has started up.");
80
        instancesStartingUp.remove(instance);
81
        startNextInstances();
82
    }
83

    
84
    protected void notifyInstanceFailed(CdmInstance instance) {
85
        logger.debug("received message that instance " + instance.getName() + " has failed.");
86
        instancesStartingUp.remove(instance);
87
        startNextInstances();
88
    }
89

    
90
    /**
91
     *
92
     */
93
    private void startNextInstances() {
94
        logger.debug("startNextInstances()");
95
        while(instancesStartingUp.size() < parallelStartUps && !isEmpty()) {
96
            CdmInstance nextInstance = pop();
97
            instancesStartingUp.add(nextInstance);
98
            logger.debug("Starting instance " + nextInstance.getName() + " in new thread.");
99
            Thread t = new StartupThread(nextInstance);
100
            t.start();
101
        }
102
    }
103

    
104
    /**
105
     * @param e
106
     */
107
    @SuppressWarnings("unused")
108
    private void registerAt(CdmInstance e) {
109
        new InstanceListener(e);
110
        startNextInstances();
111

    
112
    }
113

    
114
    class InstanceListener implements Listener {
115

    
116
        private CdmInstance instance;
117

    
118
        InstanceListener(CdmInstance instance) {
119
            this.instance = instance;
120
            instance.getWebAppContext().addLifeCycleListener(this);
121
        }
122

    
123
        /**
124
         * {@inheritDoc}
125
         */
126
        @Override
127
        public void lifeCycleStarting(LifeCycle event) {
128
            // IGNORE
129
        }
130

    
131
        /**
132
         * {@inheritDoc}
133
         */
134
        @Override
135
        public void lifeCycleStarted(LifeCycle event) {
136
            notifyInstanceStartedUp(instance);
137
            // release reference to the instance so
138
            // that the thread can be garbage collected
139
            instance.getWebAppContext().removeLifeCycleListener(this);
140
            instance = null;
141
        }
142

    
143
        /**
144
         * {@inheritDoc}
145
         */
146
        @Override
147
        public void lifeCycleFailure(LifeCycle event, Throwable cause) {
148
            notifyInstanceFailed(instance);
149
            // release reference to the instance so
150
            // that the thread can be garbage collected
151
            instance.getWebAppContext().removeLifeCycleListener(this);
152
            instance = null;
153
        }
154

    
155
        /**
156
         * {@inheritDoc}
157
         */
158
        @Override
159
        public void lifeCycleStopping(LifeCycle event) {
160
            // IGNORE
161
        }
162

    
163
        /**
164
         * {@inheritDoc}
165
         */
166
        @Override
167
        public void lifeCycleStopped(LifeCycle event) {
168
            // IGNORE
169
        }
170

    
171
    }
172

    
173
    class StartupThread extends Thread{
174

    
175
        private final Logger logger = Logger.getLogger(InstanceManager.class);
176

    
177
        private CdmInstance instance;
178

    
179
        StartupThread(CdmInstance instance){
180
            this.instance = instance;
181
        }
182

    
183
        @Override
184
        public void run() {
185
            try {
186
                instance.getWebAppContext().start();
187
                // release reference to the instance so
188
                // that the thread can be garbage collected
189
                instance = null;
190
            }
191
            catch(InterruptedException e) {
192
                try {
193
                    instance.getWebAppContext().stop();
194
                } catch (Exception e1) {
195
                    logger.error("Error on stopping instance", e1);
196
                    notifyInstanceFailed(instance);
197
                }
198
            } catch (Exception e) {
199
                logger.error("Could not start " + instance.getWebAppContext().getContextPath(), e);
200
                instance.getProblems().add(e.getMessage());
201
                instance.setStatus(Status.error);
202
                notifyInstanceFailed(instance);
203
            }
204

    
205
        }
206

    
207
    }
208

    
209
}
(6-6/7)