summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-1.6.2/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorServiceImpl.java
blob: 4d672b3a8b9fcc1faaf6399d7d05366aae762c0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.itest;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import org.osoa.sca.CallableReference;
import org.osoa.sca.annotations.Callback;
import org.osoa.sca.annotations.Destroy;
import org.osoa.sca.annotations.Scope;
import org.osoa.sca.annotations.Service;

/**
 * Sample Event Processor Service Implementation 
 */
@Service(EventProcessorService.class)
@Scope("CONVERSATION")
public class EventProcessorServiceImpl implements EventProcessorService {

    /**
     * Reference to the call back
     */
    @Callback
    protected CallableReference<EventProcessorCallBack> clientCallback;
    
    /**
     * This map contains the call backs for each of the registered Event names
     */
    private final Map<String, CallableReference<EventProcessorCallBack>> eventListeners; 
    
    /**
     * The list of all Event Generators we create
     */
    private final EventGenerator[] allEventGenerators;
    
    /**
     * Constructor. Starts the Event Generators
     */
    public EventProcessorServiceImpl() {
        eventListeners = new ConcurrentHashMap<String, CallableReference<EventProcessorCallBack>>();
        
        // We will simulate an Event generator
        allEventGenerators = new EventGenerator[2];
        allEventGenerators[0] = new EventGenerator("ONE", 1);      // Generate the SECOND event every second
        allEventGenerators[1] = new EventGenerator("FIVE", 5);     // Generate the FIVE event every 5 seconds
    }
    
    /**
     * Registers the client to receive notifications for the specified event
     * 
     * @param aEventName The name of the Event to register
     */
    public void registerForEvent(String aEventName)
    {
        // Register for the Event
        eventListeners.put(aEventName, clientCallback);
        
        // Send the "register" started event to the client
        receiveEvent(aEventName, "SameThread: Registered to receive notifications for " + aEventName);
    }

    /**
     * Unregisters the client so it no longer receives notifications for the specified event
     * 
     * @param aEventName The name of the Event to unregister
     */
    public void unregisterForEvent(String aEventName)
    {
        // Send the "register" started event to the client
        receiveEvent(aEventName, "SameThread: Unregister from receiving notifications for " + aEventName);
        
        eventListeners.remove(aEventName);
    }
    
    /**
     * This method is called whenever the EventProcessorService receives an Event
     * 
     * @param aEventName The name of the Event received
     * @param aEventData The Event data
     */
    private void receiveEvent(String aEventName, Object aEventData)
    {
        // Get the listener for the Event
        final CallableReference<EventProcessorCallBack> callback = eventListeners.get(aEventName);
        if (callback == null)
        {
            //System.out.println("No registered listeners for " + aEventName);
            return;
        }
        
        // Trigger the call back
        // System.out.println("Notifying " + callback + " of event " + aEventName);
        callback.getService().eventNotification(aEventName, aEventData);
        // System.out.println("Done notify " + callback + " of event " + aEventName);
    }
    
    /**
     * Shuts down the Event Processor
     */
    @Destroy
    public void shutdown()
    {
        System.out.println("Shutting down the EventProcessor");
        
        // Clear list of call back locations as we don't want to send any more notifications
        eventListeners.clear();

        // Stop the Event Generators
        for (EventGenerator generator : allEventGenerators)
        {
            generator.stop();
        }
    }
    
    /**
     * Utility class for generating Events
     */
    private class EventGenerator
    {
        /**
         * The Timer we are using to generate the events
         */
        private final Timer timer = new Timer();
        
        /**
         * Constructor
         *
         * @param aEventName The name of the Event to generate
         * @param frequencyInSeconds How frequently we should generate the Events
         */
        private EventGenerator(String aEventName, int frequencyInSeconds)
        {
            timer.schedule(new EventGeneratorTimerTask(aEventName), 
                    frequencyInSeconds * 1000, frequencyInSeconds * 1000);
        }
        
        /**
         * Stop this Event Generator
         */
        private void stop()
        {
            timer.cancel();
        }
        
        /**
         * The TimerTask that is invoked by the Timer for the EventGenerator
         */
        private class EventGeneratorTimerTask extends TimerTask
        {
            /**
             * The name of the Event we should generate
             */
            private final String eventName;

            /**
             * Constructor
             *
             * @param aEventName The name of the Event we should generate
             */
            private EventGeneratorTimerTask(String aEventName)
            {
                eventName = aEventName;
            }

            /**
             * Timer calls this method and it will generate an Event
             */
            @Override
            public void run()
            {
                // System.out.println("Generating new event " + eventName);
                receiveEvent(eventName, "Separate Thread Notification: " + UUID.randomUUID().toString());
            }
        }
    }
}