Project

General

Profile

« Previous | Next » 

Revision 10378

Added by Jing Tao over 7 years ago

Use a singleton class to get the messaging client.

View differences:

src/edu/ucsb/nceas/metacat/dataone/IndexTaskMessagingClientManager.java
1
package edu.ucsb.nceas.metacat.dataone;
2

  
3
import org.apache.log4j.Logger;
4
import org.dataone.cn.index.messaging.IndexTaskMessagingClient;
5
import org.dataone.cn.index.messaging.IndexTaskMessagingClientFactory;
6

  
7
/**
8
 * This class will guarantee to get a single IndexTaskMesagingClient by the singleton pattern 
9
 * @author tao
10
 *
11
 */
12
public class IndexTaskMessagingClientManager {
13

  
14
    private IndexTaskMessagingClient indexTaskClient = null;
15
    private static IndexTaskMessagingClientManager manager = null;
16
    private static Logger logMetacat = Logger.getLogger(IndexTaskMessagingClientManager.class);
17
    
18
    /**
19
     * A thread safe method to get the instance of the manager
20
     * @return
21
     * @throws InstantiationException
22
     * @throws IllegalAccessException
23
     * @throws ClassNotFoundException
24
     */
25
    public static IndexTaskMessagingClientManager getInstance() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
26
        if(manager == null) {
27
            synchronized (IndexTaskMessagingClientManager.class) {
28
                if(manager == null) {
29
                    manager = new IndexTaskMessagingClientManager();
30
                }
31
            }
32
        }
33
        return manager;
34
    }
35
    
36
    /**
37
     * Default private constructor
38
     * @throws InstantiationException
39
     * @throws IllegalAccessException
40
     * @throws ClassNotFoundException
41
     */
42
    private IndexTaskMessagingClientManager() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
43
        initIndexTaskClient();
44
    }
45
    
46
    /*
47
     * Method to get the index tasks client.
48
     */
49
    private void initIndexTaskClient() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
50
        if(indexTaskClient == null) {
51
            indexTaskClient = IndexTaskMessagingClientFactory.getClient();
52
            logMetacat.info("IndexTaskMessagingClientManager.initIndexTaskClient - after generating the client .......................");
53
        }
54
    }
55
    
56
    /**
57
     * Get the client object which can submit the index tasks.
58
     * @return
59
     */
60
    public IndexTaskMessagingClient getMessagingClient() {
61
        return indexTaskClient;
62
    }
63
}

Also available in: Unified diff