XStream Aliases Annotations

Hey everyone,

I have decided not to abandon the default marshalling of events using XStream. Instead, I have opted for the use of @XStreamAlias annotations for my classes. However, I have found that the only way that I am able to make sure that these annotations work is that I have to use my own custom serializer. I am looking for feedback to my approach, and if there are any other ideas of how I can get this to work in a more clean fashion.

I chose to extend the XStreamEventSerializer. Inside of this, I am just calling getXStream().processAnnotations(Event.class) for all of the event classes. This adds the labor of every time I create a new event, I have to remember to add it to this class. This custom serializer is then injected into my event store. If anyone has any suggestions on how to clean this up, that would be great.

Thanks
Carlus

Hi Carlus,

I use the JpaEventStore and I pass it my own instance of the
XStreamEventSerializer, instead of having the JpaEventStore create it,
so that I may add my own aliases using the addAlias method. Here is
my Spring Java Config:

  @Bean
  public JpaEventStore eventStore() {
    XStreamEventSerializer eventSerializer = new XStreamEventSerializer();
    eventSerializer.addAlias("...", ....class);
    eventSerializer.addAlias("...", ....class);

    JpaEventStore eventStore = new JpaEventStore(eventSerializer);
    return eventStore;
  }

I hope that helps.

Seamus

sevanasse,

Thanks for the quick response. I think that both of our solutions are similar. You are using the addAlias() method on the XStreamEventSerializer while I am using the annotation method, talking directly to xStream itself. However, both of us are still having to explicitly specify the event classes. In the event that we forget to specify the event classes, our events will be persisted with the default serialization (specifying the package and class). My hope is for a solution where we don’t have to specify the event classes. If the events were themselves spring beans, then I could use something like a BeanPostProcessor to invoke processAnnotations() on all event classes.

What are your thoughts if Axon gave us the ability to do this? It would just run the processAnnotations on all Event classes and if they were annotated with the @XstreamAlias, then it would render serialize the events appropriately.

Thoughts?

Hi Carlos,

How about using the XStreamEventSerializer.addPackageAlias instead and
just list the few packages you have instead of the many events?

Seamus

sevenasse,

Hey…I like that. I think that is a step in the right direction.

Thanks
Carlus

Hi Carlus,

this is how I did it in my own project. I just created an alias for each of the packages where events are located. It saves a lot of bytes, but still gives some contextual informaiton about the event itself (e.g. contacts.AddressChangedEvent).

XStream does have “Name Coders”, but they don’t seem very suitable for your purpose. So it looks like Seamus’ solution suits best.

Cheers,

Allard