You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Cristiano Costantini <cr...@gmail.com> on 2013/05/29 12:48:08 UTC

TypeConverting to null and

Hi All,

I have a custom type converter to a custom data model.

For my purposes, some input types need to be converted to null and  the
exchange need to be processed to add an header even if conversion results
in null.

For example (simplified):

@Converter
public static MyPayload convert(MessageContentsList message, Exchange
exchange) {
  if (exchange != null) {
    exchange.getIn().getHeaders().put("newHeaderName",
"someNewHeaderValue");
  }
  return null;
}

Well, the first time the converter is invoked it works (the exchange has
the new header),
The second time invokation of "convert" get skipped in
BaseTypeConverterRegistry.doConvertTo(...) as the following if statement
resolves to true:

        // check if we have tried it before and if its a miss
        TypeMapping key = new TypeMapping(type, value.getClass());
        if (misses.containsKey(key)) {
            // we have tried before but we cannot convert this one
            return Void.TYPE;
        }

I've seen that there have been "semantical ambiguity of null"  in the past,
which should have been solved in issue CAMEL-84, but I've not understood it
and how I should handle this scenario in a proper way.

I guess I'm using type converter in a bad style, but as I don't understand
why I can't guess the good way.

The only idea I have is to create a class MyEmptyPayload implements
MyPayload { } and return an instance to this instead of null, but I still
would like to understand the reasons.

Could someone orient me toward the right solution?

Thank you very much,
Cristiano

Re: TypeConverting to null and

Posted by Cristiano Costantini <cr...@gmail.com>.
thank you for your kind response. :-)
I'm satisfied now as I have a good vision and I can make the right choices
in my context.

Just for the sake of completeness,
my usage of the type converter was "inside" void process(Exchange)  method,
and I was invoking the type converter explicitly this way:

Object result = aRefelctedMethod.invoke(obj, args);
TypeConverter typeConverter = exchange.getContext().getTypeConverter();
MyPayload resultPayload = typeConverter.convertTo(MyPayload.class,
exchange, result);
exchange.getOut().setBody(resultPayload);

I used the Camel Type Converter because I'm not aware of the result Object
as it comes from a class not aware to this context.

The camel type converter is useful as it resolve which type of conversion
to use depending on the type of result.

In the end, the quickest way to fix it, is to have my "good" type converter
return an instance of "MyEmptyPayload". I could do a if(result instanceof
MyEmptyPayload) { exchange.getOut().setBody(null); } but in my case it was
best to change consumers of the exchange tolerate the MyEmptyPayload
instance.

Thank you again,
as I'm quite new to type converter API, feedback on the style is welcome!

Cristiano



2013/5/30 Christian Müller <ch...@gmail.com>

> You cannot use the type converter in this case. Camel is throwing a
> NoTypeConversionAvailableException if the type converter returns null and
> no other suitable type converter could be found. Please have a look at my
> attached sample project...
>
> I suggest to use a bean/processor afterwards to set the body to null (e.g.
> if this special header entry exists).
>
> Best,
>
> Christian Müller
> -----------------
>
> Software Integration Specialist
>
> Apache Camel committer: https://camel.apache.org/team
> V.P. Apache Camel: https://www.apache.org/foundation/
> Apache Member: https://www.apache.org/foundation/members.html
>
> https://www.linkedin.com/pub/christian-mueller/11/551/642
>
>
> On Wed, May 29, 2013 at 12:48 PM, Cristiano Costantini <
> cristiano.costantini@gmail.com> wrote:
>
>> Hi All,
>>
>> I have a custom type converter to a custom data model.
>>
>> For my purposes, some input types need to be converted to null and  the
>> exchange need to be processed to add an header even if conversion results
>> in null.
>>
>> For example (simplified):
>>
>> @Converter
>> public static MyPayload convert(MessageContentsList message, Exchange
>> exchange) {
>>   if (exchange != null) {
>>     exchange.getIn().getHeaders().put("newHeaderName",
>> "someNewHeaderValue");
>>   }
>>   return null;
>> }
>>
>> Well, the first time the converter is invoked it works (the exchange has
>> the new header),
>> The second time invokation of "convert" get skipped in
>> BaseTypeConverterRegistry.doConvertTo(...) as the following if statement
>> resolves to true:
>>
>>         // check if we have tried it before and if its a miss
>>         TypeMapping key = new TypeMapping(type, value.getClass());
>>         if (misses.containsKey(key)) {
>>             // we have tried before but we cannot convert this one
>>             return Void.TYPE;
>>         }
>>
>> I've seen that there have been "semantical ambiguity of null"  in the
>> past,
>> which should have been solved in issue CAMEL-84, but I've not understood
>> it
>> and how I should handle this scenario in a proper way.
>>
>> I guess I'm using type converter in a bad style, but as I don't understand
>> why I can't guess the good way.
>>
>> The only idea I have is to create a class MyEmptyPayload implements
>> MyPayload { } and return an instance to this instead of null, but I still
>> would like to understand the reasons.
>>
>> Could someone orient me toward the right solution?
>>
>> Thank you very much,
>> Cristiano
>>
>
>

Re: TypeConverting to null and

Posted by Christian Müller <ch...@gmail.com>.
You cannot use the type converter in this case. Camel is throwing a
NoTypeConversionAvailableException if the type converter returns null and
no other suitable type converter could be found. Please have a look at my
attached sample project...

I suggest to use a bean/processor afterwards to set the body to null (e.g.
if this special header entry exists).

Best,

Christian Müller
-----------------

Software Integration Specialist

Apache Camel committer: https://camel.apache.org/team
V.P. Apache Camel: https://www.apache.org/foundation/
Apache Member: https://www.apache.org/foundation/members.html

https://www.linkedin.com/pub/christian-mueller/11/551/642


On Wed, May 29, 2013 at 12:48 PM, Cristiano Costantini <
cristiano.costantini@gmail.com> wrote:

> Hi All,
>
> I have a custom type converter to a custom data model.
>
> For my purposes, some input types need to be converted to null and  the
> exchange need to be processed to add an header even if conversion results
> in null.
>
> For example (simplified):
>
> @Converter
> public static MyPayload convert(MessageContentsList message, Exchange
> exchange) {
>   if (exchange != null) {
>     exchange.getIn().getHeaders().put("newHeaderName",
> "someNewHeaderValue");
>   }
>   return null;
> }
>
> Well, the first time the converter is invoked it works (the exchange has
> the new header),
> The second time invokation of "convert" get skipped in
> BaseTypeConverterRegistry.doConvertTo(...) as the following if statement
> resolves to true:
>
>         // check if we have tried it before and if its a miss
>         TypeMapping key = new TypeMapping(type, value.getClass());
>         if (misses.containsKey(key)) {
>             // we have tried before but we cannot convert this one
>             return Void.TYPE;
>         }
>
> I've seen that there have been "semantical ambiguity of null"  in the past,
> which should have been solved in issue CAMEL-84, but I've not understood it
> and how I should handle this scenario in a proper way.
>
> I guess I'm using type converter in a bad style, but as I don't understand
> why I can't guess the good way.
>
> The only idea I have is to create a class MyEmptyPayload implements
> MyPayload { } and return an instance to this instead of null, but I still
> would like to understand the reasons.
>
> Could someone orient me toward the right solution?
>
> Thank you very much,
> Cristiano
>