Metadata doesn't have a session on startup when processing amqp

Hey all,

In my Cuba application I have a rabbitMQ Listener which listens to a rabbitMQ queue. When picking up a message from the queue, I convert that message to an object using the injected Metadata interface, with the method metadata.getClass(“project$Order”), and then I serialize it.

All works fine, but when I deploy a new version, on starting up, the first few messages it picks up from the queue fail due a NullPointerException.That’s because the Metadata interface doesn’t have it’s session yet, so it fails on the above method.

What would be the proper way of handling this ?

[edit] I’ve found these in the logfiles. The second line is my attempt to get the session, which normally throws the exception:

  • 12:58:35.402 INFO com.haulmont.cuba.core.sys.MetadataImpl - Initializing metadata
  • Failed attempt # 1 getting metadata.getSession()
  • 12:58:35.675 INFO com.haulmont.cuba.core.sys.MetadataImpl - Metadata initialized in 273ms

Thanks,

Jeroen

Hi Jeroen,

You should defer the event processing until the platform beans are fully initialized.

Ideally you should start the processing only after receiving AppContextStartedEvent with the order greater than Events.LOWEST_PLATFORM_PRECEDENCE.

If it’s impossible, you can try to wait until Metadata session is initialized by checking metadata.getSession() != null in a loop. However, this approach may not work until you get the platform update including this fix: More reliable Metadata session initialization · Issue #811 · cuba-platform/cuba · GitHub

1 Like

I resolved it with the second option for now, Ill dive into the first option.

Thanks Konstatin!