I am trying to create a custom out-bound connector which is going to call a third-party API.
The business logic to call the API is written in a service class. I am trying to autowire the service class to the connector class but its always coming as null, the Autowiring is not working.
I have also tried reading some configurations from application.properties using the @Value annotation it was not sucessful as well.
Can someone please help me in the same I am using camunda 8.
@Component
@OutboundConnector(
name = "custom-email-connector",
inputVariables = {"emailTo", "templateId", "body"},
type = "email-connector")
public class NotificationConnector implements OutboundConnectorFunction {
@Autowired
private EmailService emailService;
private static final Logger LOGGER = LoggerFactory.getLogger(NotificationConnector.class);
@Override
public Object execute(OutboundConnectorContext context) throws IOException, InterruptedException {
LOGGER.info("variables {}",context.getJobContext().getVariables());
final var connectorRequest = context.bindVariables(MyConnectorRequest.class);
return executeConnector(connectorRequest);
}
private MyConnectorResult executeConnector(final MyConnectorRequest connectorRequest) throws IOException, InterruptedException {
// TODO: implement connector logic
return emailService.send(new EmailRequest(connectorRequest));
}
}
to make that working, you should remove the service discovery from the class (the file located in META-INF/services) as the connector-runtime-spring has a bean discovery strategy implemented.
Also, I would advise you to use constructor injection to prevent this kind of misconfiguration.
Thanks @jonathan.lukas it really helped autowiring is working for me now, but I am still facing issues in reading configuration properties from application properties file.
@jonathan.lukas My autowiring is working with the solution you shared by removing the file from META_INF.services folder, but its only working on local runtime when I try to run it with dockerized run time the connector is not getting any hit.
@jonathan.lukas it works if I put the META_INF/services file but in that case autowiring stops working…If I remove the folder everything works as expected in local but the connector stops getting hits in docker
The spring boot application in the docker image is not aware of your component/bean when starting up as it uses the default packages for component scanning. Hence the auto-wiring is not working. When runnning using the local runtime it seems like its picked up as its part of the component scan path. More info: https://www.baeldung.com/spring-component-scanning
@shubham.parmar In your case you might want to have more control. I would recommend to look at the spring-boot-starter-camunda-connectors package that allows you to add the Connectors runtime to a spring boot application. This gives you full control over the application and its configuration and would allow you to use the @ComponentScan annotation as well.