Filter data inside custom Processor module? - spring-xd

I am writing a custom Processor module using a transformer class,
and for some input, I do not want the transformer to generate any output, how can I do that?
I tried to return null in the transform() method, but that make Spring-xd runtime generate exceptions.
And I do not want to use a Filter in the stream definition because that will be overkill for me.
Any suggestions?

Transformers always require a reply.
Use a <service-activator/> instead, with requires-reply="false".

Related

How to modify the de/serialization in protobuf compiler output?

I want to introduce a different method of deserialization and serialization when generating to protobuf files.
In essence, after running protoc -I=/home/ubuntu/protobuf-3.21.6/src -I=./ --cpp_out=./ addressbook.proto from the examples I would want to generate custom uint8_t* Person_PhoneNumber::_InternalSerialize functions.
Those functions should change the serialization method on a condition like the size of the message itself.
Ideally I would want to keep the function signatures same and use something like protobuf plugins to achieve this. The key is for the additional functionality to be be included without changing the API.
What is the best way to extend the protobuf compiler without changing the source code?

How to pass context between asynchronous method calls in Python 3.5?

How can I pass context from on asynchronous method call to another one - without using method parameters?
I need the feature to enrich log messages with a kind of flow ID so that I can easily trace all log messages of a specific call method flow.
I use Python's async and await keywords (Python 3.5.x).
You should use Context Variables introduced in Python 3.7, and I have a polyfill for Python < 3.7 as aiocontextvars.
Previous answer:
You may want to take a look at tasklocals and aiolocals.
I solved the problem by setting a custom task factory. It turned out that having a context per task (in comparison to a context per async call) was sufficient for me.
I'm working on aiotask-context package. It's a really simple way of passing context between tasks (called with await or yield from). If you don't wan't to use the package you can still use the idea :).
I'm working on how to propagate it for the ensure_future calls too.
import contextvars
c_id = contextvars.ContextVar("context_id", default=None)
def get_context_id():
return c_id.get()
def set_context_id(value):
c_id.set(value)
I struggled a lot for getting this right. If anyone is still searching for the answer then they can refer here. This works with Python3.7 onwards.
Create an instance of contextvars.ContextVar.
Here you can give the context variable name and a default value for that variable, the default value will be used in case the variable is not found in the current context.
Set the value using the setter and you can get the same value using the getter inside same context.
Define ContextVar at the top level once, and not inside closures(i.e. functions or class etc.) as garbage collection for context is not proper.

best practice to get the default object wrapper?

when creating custom method, I implements TemplateMethodModelEx and returns SimpleSequence object.
according to the API, I should use this constructor:
SimpleSequence(ObjectWrapper wrapper)
since I am setting incompatibleImprovements as 2.3.24, the doc said I can simply use Configuration instance's getObjectWrapper(). My problem is when implementing TemplateMethodModelEx, I have no access to the current config unless I pass cfg to the method's constuctor. then the root.put would look like:
root.put("getMeList", new GetMeListMethod(cfg));
this looks odd to me, i wonder whats the right to construct this kind of SimpleSquence model and whats the right way to get the default object wrapper.
Thanks a lot
You should pass in the ObjectWrapper as the constructor parameter. (It's unrelated to incompatibleImprovements 2.3.24.) Any TemplateModel that creates other TemplateModel-s (like TemplteSequenceModel-s, TemplateHashModel-s, TemplateMethodModel-s) used to work like that. This is normally not apparent because they are created by an ObjectWrapper. If you do the TemplateModel-s manually however (which is fine), then you will face this fact.

Spring state machine builder using StateTransition object

I couldn't find any reference with this functionality. Shall I just implement a helper method in the builder to read fields in StateTransition object and populate the chain configureTransition() call by myself??
Just to confirm not to reinvent the wheels.
UPDATE:
I'm trying to use StateMachineBuilder to configure with some pre-defined states and transitions in a properties file. In Builder, they use this chained call to generate configuration:
builder.configureTransitions().withExternal().source(s1)....
What I have in mind is, everything read from the file is stored in an object, the spring sm library has this StateTransition object. But as far as I know from the API, there is no way to use it directly to configure a state machine. Instead, I can read individual fields in the object and use the chained call above.
Thanks!
If you want to do it like that, what you mentioned is pretty much only option. Hopefully we get a real support for external state machine definition, i.e. tracked in https://github.com/spring-projects/spring-statemachine/issues/78.

Default implementation of a class in Gson

I am using the Gson library to make Json objects. I made a serializer for my Class. From what I see I have to use the builder and do .registerTypeAdapter on it whenever I want to use my serializer.
Is there a way around this? Is there a way to have the serializer I wrote implicitly associated with my class? If not what do people usually do to keep their code clean when using their own serializers?
Is there a way around this? Is there a way to have the serializer I wrote implicitly associated with my class?
Not with Gson.
If not what do people usually do to keep their code clean when using their own serializers?
Use Jackson. With Jackson, there are multiple ways to register serializers.

Resources