Search in sources :

Example 6 with SerializationConfig

use of org.codehaus.jackson.map.SerializationConfig in project helix by apache.

the class TestSchedulerMessage2 method testSchedulerMsg2.

@Test()
public void testSchedulerMsg2() throws Exception {
    _factory._results.clear();
    Thread.sleep(2000);
    HelixManager manager = null;
    for (int i = 0; i < NODE_NR; i++) {
        _participants[i].getMessagingService().registerMessageHandlerFactory(_factory.getMessageType(), _factory);
        // _startCMResultMap.get(hostDest)._manager;
        manager = _participants[i];
    }
    Message schedulerMessage = new Message(MessageType.SCHEDULER_MSG + "", UUID.randomUUID().toString());
    schedulerMessage.setTgtSessionId("*");
    schedulerMessage.setTgtName("CONTROLLER");
    // TODO: change it to "ADMIN" ?
    schedulerMessage.setSrcName("CONTROLLER");
    // Template for the individual message sent to each participant
    Message msg = new Message(_factory.getMessageType(), "Template");
    msg.setTgtSessionId("*");
    msg.setMsgState(MessageState.NEW);
    // Criteria to send individual messages
    Criteria cr = new Criteria();
    cr.setInstanceName("localhost_%");
    cr.setRecipientInstanceType(InstanceType.PARTICIPANT);
    cr.setSessionSpecific(false);
    cr.setResource("%");
    cr.setPartition("%");
    ObjectMapper mapper = new ObjectMapper();
    SerializationConfig serializationConfig = mapper.getSerializationConfig();
    serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);
    StringWriter sw = new StringWriter();
    mapper.writeValue(sw, cr);
    String crString = sw.toString();
    schedulerMessage.getRecord().setSimpleField("Criteria", crString);
    schedulerMessage.getRecord().setMapField("MessageTemplate", msg.getRecord().getSimpleFields());
    schedulerMessage.getRecord().setSimpleField("TIMEOUT", "-1");
    schedulerMessage.getRecord().setSimpleField("WAIT_ALL", "true");
    Criteria cr2 = new Criteria();
    cr2.setRecipientInstanceType(InstanceType.CONTROLLER);
    cr2.setInstanceName("*");
    cr2.setSessionSpecific(false);
    schedulerMessage.getRecord().setSimpleField(DefaultSchedulerMessageHandlerFactory.SCHEDULER_TASK_QUEUE, "TestSchedulerMsg2");
    TestSchedulerMessage.MockAsyncCallback callback = new TestSchedulerMessage.MockAsyncCallback();
    manager.getMessagingService().sendAndWait(cr2, schedulerMessage, callback, -1);
    String msgId = callback._message.getResultMap().get(DefaultSchedulerMessageHandlerFactory.SCHEDULER_MSG_ID);
    HelixDataAccessor helixDataAccessor = manager.getHelixDataAccessor();
    PropertyKey.Builder keyBuilder = helixDataAccessor.keyBuilder();
    for (int i = 0; i < 10; i++) {
        Thread.sleep(200);
        PropertyKey controllerTaskStatus = keyBuilder.controllerTaskStatus(MessageType.SCHEDULER_MSG.name(), msgId);
        ZNRecord statusUpdate = helixDataAccessor.getProperty(controllerTaskStatus).getRecord();
        if (statusUpdate.getMapFields().containsKey("Summary")) {
            break;
        }
    }
    Assert.assertEquals(_PARTITIONS, _factory._results.size());
    PropertyKey controllerTaskStatus = keyBuilder.controllerTaskStatus(MessageType.SCHEDULER_MSG.name(), msgId);
    ZNRecord statusUpdate = helixDataAccessor.getProperty(controllerTaskStatus).getRecord();
    Assert.assertTrue(statusUpdate.getMapField("SentMessageCount").get("MessageCount").equals("" + (_PARTITIONS * 3)));
    int messageResultCount = 0;
    for (String key : statusUpdate.getMapFields().keySet()) {
        if (key.startsWith("MessageResult ")) {
            messageResultCount++;
        }
    }
    Assert.assertEquals(messageResultCount, _PARTITIONS * 3);
    int count = 0;
    for (Set<String> val : _factory._results.values()) {
        count += val.size();
    }
    Assert.assertEquals(count, _PARTITIONS * 3);
}
Also used : HelixManager(org.apache.helix.HelixManager) Message(org.apache.helix.model.Message) SerializationConfig(org.codehaus.jackson.map.SerializationConfig) Criteria(org.apache.helix.Criteria) HelixDataAccessor(org.apache.helix.HelixDataAccessor) StringWriter(java.io.StringWriter) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 7 with SerializationConfig

use of org.codehaus.jackson.map.SerializationConfig in project helix by apache.

the class TestSchedulerMsgUsingQueue method testSchedulerMsgUsingQueue.

@Test()
public void testSchedulerMsgUsingQueue() throws Exception {
    // Logger.getRootLogger().setLevel(Level.INFO);
    _factory._results.clear();
    Thread.sleep(2000);
    HelixManager manager = null;
    for (int i = 0; i < NODE_NR; i++) {
        _participants[i].getMessagingService().registerMessageHandlerFactory(_factory.getMessageType(), _factory);
        // _startCMResultMap.get(hostDest)._manager;
        manager = _participants[i];
    }
    Message schedulerMessage = new Message(MessageType.SCHEDULER_MSG + "", UUID.randomUUID().toString());
    schedulerMessage.setTgtSessionId("*");
    schedulerMessage.setTgtName("CONTROLLER");
    // TODO: change it to "ADMIN" ?
    schedulerMessage.setSrcName("CONTROLLER");
    schedulerMessage.getRecord().setSimpleField(DefaultSchedulerMessageHandlerFactory.SCHEDULER_TASK_QUEUE, "TestSchedulerMsg");
    // Template for the individual message sent to each participant
    Message msg = new Message(_factory.getMessageType(), "Template");
    msg.setTgtSessionId("*");
    msg.setMsgState(MessageState.NEW);
    // Criteria to send individual messages
    Criteria cr = new Criteria();
    cr.setInstanceName("localhost_%");
    cr.setRecipientInstanceType(InstanceType.PARTICIPANT);
    cr.setSessionSpecific(false);
    cr.setResource("%");
    cr.setPartition("%");
    ObjectMapper mapper = new ObjectMapper();
    SerializationConfig serializationConfig = mapper.getSerializationConfig();
    serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);
    StringWriter sw = new StringWriter();
    mapper.writeValue(sw, cr);
    String crString = sw.toString();
    schedulerMessage.getRecord().setSimpleField("Criteria", crString);
    schedulerMessage.getRecord().setMapField("MessageTemplate", msg.getRecord().getSimpleFields());
    schedulerMessage.getRecord().setSimpleField("TIMEOUT", "-1");
    HelixDataAccessor helixDataAccessor = manager.getHelixDataAccessor();
    PropertyKey.Builder keyBuilder = helixDataAccessor.keyBuilder();
    helixDataAccessor.createControllerMessage(schedulerMessage);
    for (int i = 0; i < 30; i++) {
        Thread.sleep(2000);
        if (_PARTITIONS == _factory._results.size()) {
            break;
        }
    }
    Assert.assertEquals(_PARTITIONS, _factory._results.size());
    PropertyKey controllerTaskStatus = keyBuilder.controllerTaskStatus(MessageType.SCHEDULER_MSG.name(), schedulerMessage.getMsgId());
    int messageResultCount = 0;
    for (int i = 0; i < 10; i++) {
        ZNRecord statusUpdate = helixDataAccessor.getProperty(controllerTaskStatus).getRecord();
        Assert.assertTrue(statusUpdate.getMapField("SentMessageCount").get("MessageCount").equals("" + (_PARTITIONS * 3)));
        for (String key : statusUpdate.getMapFields().keySet()) {
            if (key.startsWith("MessageResult ")) {
                messageResultCount++;
            }
        }
        if (messageResultCount == _PARTITIONS * 3) {
            break;
        } else {
            Thread.sleep(2000);
        }
    }
    Assert.assertEquals(messageResultCount, _PARTITIONS * 3);
    int count = 0;
    for (Set<String> val : _factory._results.values()) {
        count += val.size();
    }
    Assert.assertEquals(count, _PARTITIONS * 3);
}
Also used : HelixManager(org.apache.helix.HelixManager) Message(org.apache.helix.model.Message) SerializationConfig(org.codehaus.jackson.map.SerializationConfig) Criteria(org.apache.helix.Criteria) HelixDataAccessor(org.apache.helix.HelixDataAccessor) StringWriter(java.io.StringWriter) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 8 with SerializationConfig

use of org.codehaus.jackson.map.SerializationConfig in project onebusaway-application-modules by camsys.

the class SiriJsonSerializer method getJson.

public String getJson(Siri siri, String callback) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(Inclusion.NON_NULL);
    mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, false);
    mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
    mapper.setDateFormat(new RFC822SimpleDateFormat());
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    SerializationConfig config = mapper.getSerializationConfig().withAnnotationIntrospector(introspector);
    mapper.setSerializationConfig(config);
    mapper.registerModule(new JacksonModule());
    String output = "";
    if (callback != null)
        output = callback + "(";
    output += mapper.writeValueAsString(siri);
    if (callback != null)
        output += ")";
    return output;
}
Also used : SerializationConfig(org.codehaus.jackson.map.SerializationConfig) JaxbAnnotationIntrospector(org.codehaus.jackson.xc.JaxbAnnotationIntrospector) AnnotationIntrospector(org.codehaus.jackson.map.AnnotationIntrospector) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) JaxbAnnotationIntrospector(org.codehaus.jackson.xc.JaxbAnnotationIntrospector)

Example 9 with SerializationConfig

use of org.codehaus.jackson.map.SerializationConfig in project ovirt-engine by oVirt.

the class V3CustomObjectMapper method addSerializationConfig.

protected V3CustomObjectMapper addSerializationConfig() {
    // We need the instrospector that takes into account the JAXB annotations,
    // both for the serializer and for the deserializer:
    JaxbAnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    // Configure the serializer:
    SerializationConfig serCfg = getSerializationConfig().withAnnotationIntrospector(introspector);
    setSerializationConfig(serCfg);
    // Configure the deserializer:
    DeserializationConfig deserCfg = getDeserializationConfig().withAnnotationIntrospector(introspector);
    setDeserializationConfig(deserCfg);
    return this;
}
Also used : SerializationConfig(org.codehaus.jackson.map.SerializationConfig) DeserializationConfig(org.codehaus.jackson.map.DeserializationConfig) JaxbAnnotationIntrospector(org.codehaus.jackson.xc.JaxbAnnotationIntrospector)

Example 10 with SerializationConfig

use of org.codehaus.jackson.map.SerializationConfig in project ovirt-engine by oVirt.

the class CustomObjectMapper method addSerializationConfig.

protected CustomObjectMapper addSerializationConfig() {
    // We need the instrospector that takes into account the JAXB annotations,
    // both for the serializer and for the deserializer:
    JaxbAnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    // Configure the serializer:
    SerializationConfig serCfg = getSerializationConfig().withAnnotationIntrospector(introspector);
    setSerializationConfig(serCfg);
    // Configure the deserializer:
    DeserializationConfig deserCfg = getDeserializationConfig().withAnnotationIntrospector(introspector);
    setDeserializationConfig(deserCfg);
    return this;
}
Also used : SerializationConfig(org.codehaus.jackson.map.SerializationConfig) DeserializationConfig(org.codehaus.jackson.map.DeserializationConfig) JaxbAnnotationIntrospector(org.codehaus.jackson.xc.JaxbAnnotationIntrospector)

Aggregations

SerializationConfig (org.codehaus.jackson.map.SerializationConfig)19 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)16 StringWriter (java.io.StringWriter)13 ZNRecord (org.apache.helix.ZNRecord)8 Criteria (org.apache.helix.Criteria)7 HelixDataAccessor (org.apache.helix.HelixDataAccessor)7 HelixManager (org.apache.helix.HelixManager)7 PropertyKey (org.apache.helix.PropertyKey)7 Message (org.apache.helix.model.Message)7 Test (org.testng.annotations.Test)7 Builder (org.apache.helix.PropertyKey.Builder)4 PropertyPathBuilder (org.apache.helix.PropertyPathBuilder)4 JaxbAnnotationIntrospector (org.codehaus.jackson.xc.JaxbAnnotationIntrospector)4 HelixException (org.apache.helix.HelixException)3 IOException (java.io.IOException)2 TreeMap (java.util.TreeMap)2 ConstraintItem (org.apache.helix.model.ConstraintItem)2 AnnotationIntrospector (org.codehaus.jackson.map.AnnotationIntrospector)2 DeserializationConfig (org.codehaus.jackson.map.DeserializationConfig)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1