Mule - call a controller from inside onCall() method of java class - java

I am working on a project which is a mavenized web application having Mule support in it. There is requirement to call controller from inside a java class which implements Callable interface. I achieved it by creating an object of controller but it is against MVC rules. Then i tried to use #Autowired annotation but it doesn't work in onCall() method. Is there any solution by which i can call a controller method from java class?

Use Spring to inject the controller into a field of the Callable bean.

Related

How to register a bean in Quarkus Main method

I have a Quarkus application that works in batch mode. I have a QuarkusMain class, with a method that implements QuarkusApplication interface.
That method is a static method, so I'm not able to inject Beans because they have a null value.
What is the way to inject Beans in Quarkus from a static method in a QuarkusMain class?
Thanks.
Are you really after creating beans in the static method, or more just after how to startup Quarkus ?
In main method the only thing you need to do is call Quarkus.run(YourMain.class, args); which then will make YourMain class a bean and in here allow #Inject.
You can also use picocli which also ends up with a class you can use #Inject.
Example of that I have here: https://github.com/jbangdev/jbang-catalog/blob/master/catalog2readme.java
You can use Arc class in this way:
Annotate your beans with a #Startup annotation.
https://quarkus.io/guides/lifecycle#startup_annotation
Then, in your static method:
YourBean yourBean = Arc.container().instance(YourBean.class).get();

Micrometer #Timed for method in pojo not working

I am trying to instrument a method in a POJO class. When I use micrometer's #Timed annotation for a method in Spring controller class, it works fine and I can see metrics in prometheus dashboard(I have micrometer-registry-prometheus configured in pom). But when I use same annotation for a method in a POJO class(in same spring application) I don't see metrics in prometheus dashboard.
Do I need some extra config to get it working for a method in POJO class?
Edit: I have named metrics as
#Timed(value="myCustomMethod_responseTime")
Controllers are instrumented automatically, you don't need #Timed on them.
Also, you need a few things to make this work:
You need to create a TimedAspect #Bean
The method you are instrumenting should be public and belong to a #Bean

How to Mock Custom Jackson Deserializer Response in Spring Boot 1.5.9

In a Spring Boot 1.5.9 project, I'm currently trying to use #WebMvcTest to run a test against one of my controllers. The entity being operated on has a #JsonDeserializer() annotation on one of its properties pointing to a custom class. I'm attempting to mock the result of the deserialize() call in a test without invoking the body.
However, when trying to do the following, I'm getting a NullPointerException error on a line within the deserialize() method, which suggests the actual method body is being executed:
#Autowired
private MockMvc mvc;
#MockBean
private MyDeserializer myDeserializer
[...]
#Test
public void myTestMethod() {
doReturn(myDeserializedValue)
.when(myDeserializer)
.deserialize(
any(JsonParser.class),
any(DeserializationContext.class)
);
this.mvc.perform([...]) // perform mvc call that would invoke myDeserializer
logger.debug("Call complete"); // never gets to this line
}
I'm assuming the custom deserializer class is being invoked (possibly newed up) outside of the knowledge of Spring's ApplicationContext.
Is there any way to mock a custom deserializer, or do I need to bump this class up to use the full ApplicationContext via #SpringBootTest and let it fully execute?
If you want Jackson to use a specific deserializer object instance you need to register it via a module on the ObjectMapper instance. See the docs for an example with a serializer; you'll have to modify it slightly for your deserializer.
Otherwise, I assume Jackson will just instantiate a new instance of your class every time and never use your mock (or bean?) at all.

Controller beanPostProcessor spring - not singleton?

I have a custom beanPostProcessor that is supposed to do some logic with spring controllers.
I found out that it behaves funny. When I sysout controller bean in my post processor it says:
...controller.MassUploadController#743479ce
but when I call sysout(this) from controller request itself it is a different instance:
...controller.MassUploadController#58469848
So any changes I perform to controller instance in beanPostProcessor are not really reflected in runtime since it is a different instance. How is that possible? It states everywhere that spring controllers are singletons!
My controllers are defined in spring with #Controller and in xml I use componentScan on controller. java package

Injecting a bean with #Autowired not working in Soap Handler Issue?

Hi I have a SOAP Handler class which is handling the logging for request which we get for a Webservice API. I am trying to insert this request in DB inside SOAP Handler. I have created DAOs which i m trying to inject through #Autowired annotation which are used to insert request in DB. But it is giving me Null Pointer Exception instead. Why does #Autowired not work here while it works perfectly well my #Service classes.
Was your SOAP Handler been instanciated by Spring ?
Your #Service classes are read and instanciated by Spring at DI Container initialization, so the #Autowired can work.
So your handler need to be instancied by Spring to be managed (with a #Component-like annotation, for example). If it need to be instanciated by a Java EE container, you should consider extending SpringBeanAutowiringSupport
Regards

Categories