Search in sources :

Example 1 with MessageHandler

use of org.apache.helix.messaging.handling.MessageHandler in project helix by apache.

the class ScheduledTaskStateModel method onBecomeCompletedFromOffline.

@Transition(to = "COMPLETED", from = "OFFLINE")
public void onBecomeCompletedFromOffline(Message message, NotificationContext context) throws InterruptedException {
    logger.info(_partitionKey + " onBecomeCompletedFromOffline");
    // Construct the inner task message from the mapfields of scheduledTaskQueue resource group
    Map<String, String> messageInfo = message.getRecord().getMapField(Message.Attributes.INNER_MESSAGE.toString());
    ZNRecord record = new ZNRecord(_partitionKey);
    record.getSimpleFields().putAll(messageInfo);
    Message taskMessage = new Message(record);
    if (logger.isDebugEnabled()) {
        logger.debug(taskMessage.getRecord().getSimpleFields().toString());
    }
    MessageHandler handler = _executor.createMessageHandler(taskMessage, new NotificationContext(null));
    if (handler == null) {
        throw new HelixException("Task message " + taskMessage.getMsgType() + " handler not found, task id " + _partitionKey);
    }
    // Invoke the internal handler to complete the task
    handler.handleMessage();
    logger.info(_partitionKey + " onBecomeCompletedFromOffline completed");
}
Also used : NotificationContext(org.apache.helix.NotificationContext) HelixException(org.apache.helix.HelixException) Message(org.apache.helix.model.Message) MessageHandler(org.apache.helix.messaging.handling.MessageHandler) ZNRecord(org.apache.helix.ZNRecord)

Example 2 with MessageHandler

use of org.apache.helix.messaging.handling.MessageHandler in project helix by apache.

the class TestDefaultControllerMsgHandlerFactory method testDefaultControllerMsgHandlerFactory.

@Test()
public void testDefaultControllerMsgHandlerFactory() {
    System.out.println("START TestDefaultControllerMsgHandlerFactory at " + new Date(System.currentTimeMillis()));
    DefaultControllerMessageHandlerFactory facotry = new DefaultControllerMessageHandlerFactory();
    Message message = new Message(MessageType.NO_OP, "0");
    NotificationContext context = new NotificationContext(null);
    boolean exceptionCaught = false;
    try {
        MessageHandler handler = facotry.createHandler(message, context);
    } catch (HelixException e) {
        exceptionCaught = true;
    }
    AssertJUnit.assertTrue(exceptionCaught);
    message = new Message(MessageType.CONTROLLER_MSG, "1");
    exceptionCaught = false;
    try {
        MessageHandler handler = facotry.createHandler(message, context);
    } catch (HelixException e) {
        exceptionCaught = true;
    }
    AssertJUnit.assertFalse(exceptionCaught);
    Map<String, String> resultMap = new HashMap<String, String>();
    message = new Message(MessageType.NO_OP, "3");
    DefaultControllerMessageHandler defaultHandler = new DefaultControllerMessageHandler(message, context);
    try {
        defaultHandler.handleMessage();
    } catch (HelixException e) {
        exceptionCaught = true;
    } catch (InterruptedException e) {
        LOG.error("Interrupted handling message", e);
    }
    AssertJUnit.assertTrue(exceptionCaught);
    message = new Message(MessageType.CONTROLLER_MSG, "4");
    defaultHandler = new DefaultControllerMessageHandler(message, context);
    exceptionCaught = false;
    try {
        defaultHandler.handleMessage();
    } catch (HelixException e) {
        exceptionCaught = true;
    } catch (InterruptedException e) {
        LOG.error("Interrupted handling message", e);
    }
    AssertJUnit.assertFalse(exceptionCaught);
    System.out.println("END TestDefaultControllerMsgHandlerFactory at " + new Date(System.currentTimeMillis()));
}
Also used : NotificationContext(org.apache.helix.NotificationContext) HelixException(org.apache.helix.HelixException) Message(org.apache.helix.model.Message) DefaultControllerMessageHandler(org.apache.helix.manager.zk.DefaultControllerMessageHandlerFactory.DefaultControllerMessageHandler) MessageHandler(org.apache.helix.messaging.handling.MessageHandler) HashMap(java.util.HashMap) DefaultControllerMessageHandlerFactory(org.apache.helix.manager.zk.DefaultControllerMessageHandlerFactory) DefaultControllerMessageHandler(org.apache.helix.manager.zk.DefaultControllerMessageHandlerFactory.DefaultControllerMessageHandler) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 3 with MessageHandler

use of org.apache.helix.messaging.handling.MessageHandler in project helix by apache.

the class TestAsyncCallbackSvc method testAsyncCallbackSvc.

@Test(groups = { "unitTest" })
public void testAsyncCallbackSvc() throws Exception {
    AsyncCallbackService svc = new AsyncCallbackService();
    HelixManager manager = new MockHelixManager();
    NotificationContext changeContext = new NotificationContext(manager);
    Message msg = new Message(svc.getMessageType(), UUID.randomUUID().toString());
    msg.setTgtSessionId(manager.getSessionId());
    try {
        MessageHandler aHandler = svc.createHandler(msg, changeContext);
    } catch (HelixException e) {
        AssertJUnit.assertTrue(e.getMessage().indexOf(msg.getMsgId()) != -1);
    }
    Message msg2 = new Message("RandomType", UUID.randomUUID().toString());
    msg2.setTgtSessionId(manager.getSessionId());
    try {
        MessageHandler aHandler = svc.createHandler(msg2, changeContext);
    } catch (HelixException e) {
        AssertJUnit.assertTrue(e.getMessage().indexOf(msg2.getMsgId()) != -1);
    }
    Message msg3 = new Message(svc.getMessageType(), UUID.randomUUID().toString());
    msg3.setTgtSessionId(manager.getSessionId());
    msg3.setCorrelationId("wfwegw");
    try {
        MessageHandler aHandler = svc.createHandler(msg3, changeContext);
    } catch (HelixException e) {
        AssertJUnit.assertTrue(e.getMessage().indexOf(msg3.getMsgId()) != -1);
    }
    TestAsyncCallback callback = new TestAsyncCallback();
    String corrId = UUID.randomUUID().toString();
    svc.registerAsyncCallback(corrId, new TestAsyncCallback());
    svc.registerAsyncCallback(corrId, callback);
    List<Message> msgSent = new ArrayList<Message>();
    msgSent.add(new Message("Test", UUID.randomUUID().toString()));
    callback.setMessagesSent(msgSent);
    msg = new Message(svc.getMessageType(), UUID.randomUUID().toString());
    msg.setTgtSessionId("*");
    msg.setCorrelationId(corrId);
    MessageHandler aHandler = svc.createHandler(msg, changeContext);
    Map<String, String> resultMap = new HashMap<String, String>();
    aHandler.handleMessage();
    AssertJUnit.assertTrue(callback.isDone());
    AssertJUnit.assertTrue(callback._repliedMessageId.contains(msg.getMsgId()));
}
Also used : HelixManager(org.apache.helix.HelixManager) Message(org.apache.helix.model.Message) MessageHandler(org.apache.helix.messaging.handling.MessageHandler) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AsyncCallbackService(org.apache.helix.messaging.handling.AsyncCallbackService) NotificationContext(org.apache.helix.NotificationContext) HelixException(org.apache.helix.HelixException) Test(org.testng.annotations.Test)

Aggregations

HelixException (org.apache.helix.HelixException)3 NotificationContext (org.apache.helix.NotificationContext)3 MessageHandler (org.apache.helix.messaging.handling.MessageHandler)3 Message (org.apache.helix.model.Message)3 HashMap (java.util.HashMap)2 Test (org.testng.annotations.Test)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HelixManager (org.apache.helix.HelixManager)1 ZNRecord (org.apache.helix.ZNRecord)1 DefaultControllerMessageHandlerFactory (org.apache.helix.manager.zk.DefaultControllerMessageHandlerFactory)1 DefaultControllerMessageHandler (org.apache.helix.manager.zk.DefaultControllerMessageHandlerFactory.DefaultControllerMessageHandler)1 AsyncCallbackService (org.apache.helix.messaging.handling.AsyncCallbackService)1