1 package org.apache.helix.messaging.handling;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.helix.NotificationContext;
23 import org.apache.helix.messaging.handling.MessageHandler.ErrorCode;
24 import org.apache.helix.messaging.handling.MessageHandler.ErrorType;
25 import org.apache.helix.model.Message;
26
27 /**
28 * Provides the base class for all message handlers.
29 *
30 */
31 public abstract class MessageHandler
32 {
33 public enum ErrorType
34 {
35 FRAMEWORK, INTERNAL
36 }
37
38 public enum ErrorCode
39 {
40 ERROR, CANCEL, TIMEOUT
41 }
42 /**
43 * The message to be handled
44 */
45 protected final Message _message;
46
47 /**
48 * The context for handling the message. The cluster manager interface can be
49 * accessed from NotificationContext
50 */
51 protected final NotificationContext _notificationContext;
52
53 /**
54 * The constructor. The message and notification context must be provided via
55 * creation.
56 */
57 public MessageHandler(Message message, NotificationContext context)
58 {
59 _message = message;
60 _notificationContext = context;
61 }
62
63 /**
64 * Message handling routine. The function is called in a thread pool task in
65 * CMTaskExecutor
66 *
67 * @return returns the CMTaskResult which contains info about the message processing.
68 */
69 public abstract HelixTaskResult handleMessage() throws InterruptedException;
70
71 /**
72 * Callback when error happens in the message handling pipeline.
73 * @param type TODO
74 * @param retryCountLeft - The number of retries that the framework will
75 * continue trying to handle the message
76 * @param ErrorType - denote if the exception happens in framework or happens in the
77 * customer's code
78 */
79 public abstract void onError(Exception e, ErrorCode code, ErrorType type);
80
81 /**
82 * Callback when the framework is about to interrupt the message handler
83 */
84 public void onTimeout()
85 {
86
87 }
88 }