Hey,
I am trying to create an EventListener in Data Services, but it won’t register. This is my code:
Entrypoint (BapApplicationServer.java)
package com.mgmtp.a12.template.server;
import com.mgmtp.a12.dataservices.server.app.ServerApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
class BapServerApplication extends ServerApplication {
// Workaround for "NoSuchMethodException: main()" when starting in Docker
public static void main(String[] args) {
ServerApplication.main(args);
}
}
Class with the EventListener logic
package com.mgmtp.a12.template.server;
import com.mgmtp.a12.dataservices.attachment.events.AttachmentAfterCreateEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
@Component
public class PDFAttachmentListener {
@EventListener
public void afterCreateListener(AttachmentAfterCreateEvent event) throws IOException {
System.out.println("triggered")
String path = event.getAttachment().getAbsolutePath();
System.out.println(path);
// other logic
}
}
Are there some special steps I need to do? Shouldn’t Spring automatically find the Component and register the EventListener? Both classes are in the same package, so that shouldn’t really be a problem?
Hi @anon11273509,
The events are not published in different instances. If you want to create a DS extension where you will implement @EventListeneryou also need to include component scanning from DS packages and define dependencies to DS BOM files. Please see the fullstack project template how it is done there. I.e. this is our example of how to create DS extension:
buildscript {
dependencies {
}
}
description = 'examples-extending-server'
configurations {
bootExtendedServerJar.canBeResolved = false
}
artifacts {
bootExtendedServerJar(tasks.bootJar)
}
dependencies {
implementation project(':dataservices-server-app')
implementation(libs.uaaAuthenticationUserExtension) { exclude group: 'jakarta.servlet', module: 'jakarta.servlet-api' }
implementation libs.uaaAuthorization
implementation libs.uaaAuthorizationA12Extension
implementation libs.hazelcast
implementation libs.hazelcastSpring
implementation libs.hazelcastHibernate
compileOnly libs.springBootDevtools
testImplementation libs.testng
testImplementation libs.springTest
testImplementation libs.springBootTest
testImplementation libs.springSecurityTest
testImplementation libs.hamcrest
bootExtendedServerJarTask(project(path: ':examples:examples-extending-server', configuration: 'bootExtendedServerJar'))
}
springBoot {
mainClass.set 'com.mgmtp.a12.dataservices.server.app.ServerApplication'
}
bootRun {
systemProperties 'workDir': "${project.rootDir}/"
}
I have gutted this from our example-server so I hope I have not made some mistakes 
Moin,
the Eventlisteners are only picked up from dataservices packages, as configured in this version of the project template. (This was change in the later release of the Project Template 202206.3.0)
This should be fixed by doing the following two things:
- Change the mainClass in
server/app/build.gradle
mainClass = 'com.mgmtp.a12.dataservices.server.app.ServerApplication'
to this:
mainClass = 'com.mgmtp.a12.template.server.BapServerApplication'
- Change the
BapServerApplication to not use the dataservices spring application anymore
package com.mgmtp.a12.template.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
class BapServerApplication {
public static void main(String[] args) {
SpringApplication.run(BapServerApplication.class, args);
}
}