I am using quarkus, and most of my application is used to answer to Http requests.
I have some entities (@Entity Device in this case), and I am able to use persistency (write, update, delete) from every endpoint.
However, now I am using a Mqtt client, where I am subscriving some events.
@Transactional
@ApplicationScoped
@ActivateRequestContext
public class Mqtt5ClientConnector {
// constructor, connection methods, etc
static class SamplePublishEvents implements Mqtt5ClientOptions.PublishEvents {
CountDownLatch messagesReceived;
SamplePublishEvents(int messageCount) {
messagesReceived = new CountDownLatch(messageCount);
}
@Override
// @Transactional
public void onMessageReceived(Mqtt5Client client, PublishReturn publishReturn) {
PublishPacket publishPacket = publishReturn.getPublishPacket();
String publishedTopic = publishPacket.getTopic();
String eventPayload = new String(publishPacket.getPayload());
System.out.println("Publish received on topic: " + publishedTopic);
System.out.println("Message: " + eventPayload);
//do some stuff
Device.find("id", "1234"); //works
Device new_device = new Device();
new_device.id = "12345";
new_device.persist(); //does not work
}
}
// everytime I initialize the quarkus application I connect to mqqt and subscrive some topics
@Startup
@ActivateRequestContext
void init(){
client = builder.build();
client.start();
/* Connect */
try {
lifecycleEvents.connectedFuture.get(5, TimeUnit.SECONDS);
} catch (Exception ex) {
throw new RuntimeException("Exception occurred during connect", ex);
}
/* Subscribe */
SubscribePacket.SubscribePacketBuilder subscribeBuilder = new SubscribePacket.SubscribePacketBuilder();
subscribeBuilder.withSubscription("topic", QOS.AT_LEAST_ONCE, false, false, SubscribePacket.RetainHandlingType.DONT_SEND);
subscribeBuilder.withSubscription("topic2", QOS.AT_LEAST_ONCE, false, false, SubscribePacket.RetainHandlingType.DONT_SEND);
try {
client.subscribe(subscribeBuilder.build()).get(5, TimeUnit.SECONDS);
} catch (Exception ex) {
System.out.println("Subscription failed");
onApplicationFailure(ex);
}
}
}
Every event I receive I am not able to persist new information in the database, but I can read data from the database.
The error I get is the following:
jakarta.enterprise.inject.CreationException: java.lang.ClassNotFoundException: io/quarkus/hibernate/orm/runtime/TransactionSessions
at org.hibernate.Session_OpdLahisOZ9nWRPXMsEFQmQU03A_Synthetic_Bean.create(Unknown Source)
What do I need to do to be able to persist when I receive an mqtt event?