How to find out why #ExceptionHandler don't work? - java

In my project, when I add a exception handler method in my controller, it doesn't work. But if I move this code to a demo project which have the same Spring version, missingParamterHandler method works well. Can anyone help me handle this problem?
#Controller
#RequestMapping("/order")
public class OrderControlle{
#ExceptionHandler(Exception.class)
public #ResponseBody ClientResult missingParamterHandler(Exception exception) {
/*inspect and exception and obtain meaningful message*/
ClientResult clientResult = new ClientResult();
clientResult.getBstatus().setCode(BstatusCode.PARAM_ERR);
return clientResult;
}
}
I try debug, and find in Spring's DispatcherServlet.java, matchingBeans.isEmpty() returns true, is it the reason #ExceptionHandler(Exception.class) not work in my project?
private void initHandlerExceptionResolvers(ApplicationContext context) {
this.handlerExceptionResolvers = null;
if (this.detectAllHandlerExceptionResolvers) {
// Find all HandlerExceptionResolvers in the ApplicationContext, including ancestor contexts.
Map<String, HandlerExceptionResolver> matchingBeans = BeanFactoryUtils
.beansOfTypeIncludingAncestors(context, HandlerExceptionResolver.class, true, false);
if (!matchingBeans.isEmpty()) {
this.handlerExceptionResolvers = new ArrayList<HandlerExceptionResolver>(matchingBeans.values());
// We keep HandlerExceptionResolvers in sorted order.
OrderComparator.sort(this.handlerExceptionResolvers);
}
}
.....

explicit add <bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver" /> or <mvc:annotation-driven /> to app-context.xml, solve my problem.
without this above line, spring add ExceptionResolvers as below in my project.
org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,\
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver

Related

Resilience4j Not Ignoring Exceptions

JobServiceImpl.java:
#CircuitBreaker(name = "jobsApiServiceGetAllJobs", fallbackMethod = "getAllJobsFallback")
public ResponseEntity<JobsResponse> getAllJobs() {
...
throw new ApiException();
}
public ResponseEntity<JobsResponse> getAllJobsFallback(Throwable throwable) {
log.error("Fallback method called");
}
application.properties:
resilience4j.circuitbreaker.instances.jobsApiServiceGetAllJobs.ignoreExceptions=ccp.shared.platform.exception.ApiException,ccp.shared.platform.exception.BadRequestApiException
Whenever ccp.shared.platform.exception.ApiException is thrown, the fallback method is called even though I have added it in the ignoreExceptions list in the application.properties file. I want it to not trigger the fallback method when ApiException is thrown. I have tried similar questions on stack overflow and those does not work for me. Am I doing anything wrong?
Seems that the property is defined in the docs but actually, it does not work. There is a pull request for that -> CircuitBreakerConfigurationProperties has no ignoreException property.
You can use the common Spring+java configuration to achieve that in the meantime:
#Bean
CircuitBreaker reportingApiCircuitBreaker(CircuitBreakerRegistry registry) {
CircuitBreakerConfig config = CircuitBreakerConfig.custom()
.ignoreExceptions(ApiException.class, BadRequestApiException.class)
.build();
return registry.circuitBreaker("yourCircuitBreakername", config);
}

#Transactionnal annotation in spring boot does it really create a transaction?

I want to call different methods that interact with my database in one method.
something like this :
#Autowired
EnteteService es; // service for jpaRepository
#Autowired
SystemOracleServiceJpa so; // service using jdbcTemplate
#Autowired
DetailService ds; // service for jpaRepository
#Transactional
public void configure(EnteteAR entete) throws ConfigurationException {
try{
this.es.save(entete); // first methode
List<DetailAR> details = this.so.getStructure(entete.getTableName());
if(details.size()>0){
this.ds.saveAllDetails(details); // second
this.so.CreateTable(details, entete.getTableName(), "DEM");//third
this.so.createPkIdxDem(entete.getTableName()); // fourth
this.so.CreateTable(details, entete.getTableName(), "BACK"); // fifth
}
else{
throw new ConfigurationException("Configuration error");
}
}catch(Exception e){
throw new ConfigurationException(e.getMessage());
}
}
I want to commit only if no errors appears in all this methods inside my main method "configure".
I was thinking that #transactionnal annotation work for this, but that commit after each method inside.
Exemple :
if this.es.save work and this.ds.saveAllDetails dont, I find data of es.save on database :(
Someone can help my please ?
thank with advance for your reading and your potential help.
#Transactional will automatically invoke a rollback if an unchecked exception is thrown from the executed method.
ConfigurationException in your case is a checked exception and hence it does not work.
You can make it work by modifying your annotation to
#Transactional(rollbackOn = ConfigurationException.class)
public void configure(EnteteAR entete) throws ConfigurationException {
try{ ....

hystrix javanica collapser did not work

I am using hystrix javanica collapser in spring boot, but I found it did not work, my code just like this below:
service class:
public class TestService {
#HystrixCollapser(batchMethod = "getStrList")
public Future<String> getStr(String id) {
System.out.println("single");
return null;
}
#HystrixCommand
public List<String> getStrList(List<String> ids) {
System.out.println("batch,size=" + ids.size());
List<String> strList = Lists.newArrayList();
ids.forEach(id -> strList.add("test"));
return strList;
}
}
where I use:
public static void main(String[] args) {
TestService testService = new TestService();
HystrixRequestContext context = HystrixRequestContext.initializeContext();
Future<String> f1= testService.getStr("111");
Future<String> f2= testService.getStr("222");
try {
Thread.sleep(3000);
System.out.println(f1.get()); // nothing printed
System.out.println(f2.get()); // nothing printed
} catch (Exception e) {
}
context.shutdown();
}
It printed 3 single instead of 1 batch.
I want to know what's wrong with my code, a valid example is better.
I can't find a hystrix javanica sample on the internet, So I have to read the source code to solve this problem, now it's solved, and this is my summary:
when you use hystrix(javanica) collapser in spring-boot, you have to:
Defined a hystrixAspect spring bean and import hystrix-strategy.xml;
Annotate single method with #Hystrix Collapser annotate batch method with #HystrixCommand;
Make your single method need 1 parameter(ArgType) return Future , batch method need List return List and make sure size of args be equal to size of return.
Set hystrix properties batchMethod, scope, if you want to collapse requests from multiple user threads, you must set the scope to GLOBAL;
Before you submit a single request, you must init the hystrix context with HystrixRequestContext.initializeContext(), and shutdown the context when your request finish;

#transactional rollback not working

Below is what I did, I need to implement rollback, using #transactional annotation, but not working as expected, what else need to be done for proper rollback to happen ?? I want that when the code is executed result in db should be "testingOne" , currently it is set to "notRollBacked". Can you please point my mistake.
public Response deleteUser(Request argVO)throws Exception
{
Users users = UsersLocalServiceUtil.getUsers("sagar");
users.setUserName("testingOne");
UsersLocalServiceUtil.updateUsers(users);
try
{
testRollbackFunction();
}
catch (Exception ex)
{
}
return new Response();
}
#Transactional(isolation = Isolation.PORTAL, rollbackFor =
{PortalException.class, SystemException.class})
private void testRollbackFunction() throws Exception
{
Users users = UsersLocalServiceUtil.getUsers("sagar");
users.setUserName("notRollbacked");
UsersLocalServiceUtil.updateUsers(users);
throw new PortalException();
}
****************Edit 1*************
I did what was mentioned in answers:
I did taken bean from context
and written a class/bean as
#Transactional(isolation = Isolation.PORTAL, rollbackFor =
{PortalException.class, SystemException.class})
public class RollBack
{
#Transactional(isolation = Isolation.PORTAL, rollbackFor =
{PortalException.class, SystemException.class})
public void thisWillRollBack() throws Exception
{
Users users = UsersLocalServiceUtil.getUsers("sagar");
users.setBarringReason("notRollbacked");
UsersLocalServiceUtil.updateUsers(users);
throw new PortalException();
}
}
spring xml file bean refrence set as :
<bean id="rollBackBean" class="com.alepo.RollBack">
</bean>
public Response myMethod(Request argVO)throws Exception
{
Users users = UsersLocalServiceUtil.getUsers("sagar");
users.setBarringReason("testingOne");
UsersLocalServiceUtil.updateUsers(users);
try
{
Test test = new Test();
Object obj = test.getBean();
RollBack rollBack = (RollBack)obj;
rollBack.thisWillRollBack();
}
catch (Exception ex)
{
ex.printStackTrace();
}
return new Response();
}
#################EDIT 4
now calling rollback function as :
RollBack rollBack = (RollBack)PortalBeanLocatorUtil.getBeanLocator().locate("rollBackBean");
rollBack.thisWillRollBack();
No Test class in picture now ...no new anywhere ...
still NOT WORKING .......
If you have a #Transactional annotation on method, Spring wraps the call to this method with aspect handling the transaction.
So:
1) Only public methodes can be wrapped in aspect
2) You call wrapped code only if you call the method on a bean taken from / injected by Spring container.
In your case:
1) The code isn't wrapped in transactional aspect because it is not public method
2) Event if it was, it is called directly from within the class, so you wouldn't call wrapped version anyway.
So the solution is to make separate bean with #Transactional method, inject it into and call it from Response class.
Of course you need <tx:annotation-driven/> in your spring-xml or instruct Spring otherwise to process #Transactional annotations (see the reference).
The issue is you are outside the application context. You are creating a new instance of a class, NEW is bad in Spring, very bad. Get an instance of Test from the application context, not by creating a new instance unless you start your application context in Test. Try to Autowire test in your class you mention above or inject it from Spring and then let me know, but the code you are showing above will never work with transaction management.

Refreshing Grails applicationContext

In my configuration's spring/resources.xml file, I define a bean like this :
<bean id="myService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://${remote.host}:8080/MyAgent/remoting/MyService"/>
<property name="serviceInterface" value="services.MyService"/>
</bean>
In my Config.groovy file I have :
remote.host = "someipaddress"
Now I'd like to change this placeholder's value at runtime. In a regular spring app, I do this through a PropertyPlaceHolderConfigurer, then I refresh the context and it works.
In Grails, how can I refresh the context ?
Regards,
Philippe
Ok I give up the refreshing approach.
As a workaround, I created a grails service that looks like this :
class myService {
def myRemoteService
static transactional = false
private MyRemoteService getService(String remoteServiceURL) {
HessianProxyFactory factory = new HessianProxyFactory();
try {
return (MyRemoteService) factory.create(MyRemoteService.class, url);
}
catch (MalformedURLException e) {
e.printStackTrace()
}
return null
}
def someRemoteMethod(String remoteServiceURL) {
getService(remoteServiceURL).myRemoteMethod()
}
}
This allows me to invoke the remote service on any distant machine dinamically.
I'm still interested in a cleaner solution as this makes me rewrite a wrapper method for each remote method :-S
Why not just update the value directly:
def blabla
...
void someServiceMethod() {
blabla.someProperty = 'new value'
}
or
def blabla
...
def someControllerAction = {
blabla.someProperty = 'new value'
}
grailsApplication expose a refresh() method, i'm not sure if it will reload spring context, you could try.
I did a quick search in grails mailing list, and looks like grails do not support app-context reload.
You could try implement InitializingBean and get the values direct from app config.
import org.springframework.beans.factory.InitializingBean
class ExampleService implements InitializingBean {
def grailsApplication
def setting
void afterPropertiesSet() {
this.setting = grailsApplication.config.setting
}
}
Maybe you can listen for changes in the config or get the property every time you need to use it, i do not know, i can not create a app to run some tests right now.
Not tested, but try:
import grails.spring.BeanBuilder
def bb = new BeanBuilder(
application.parentContext,
new GroovyClassLoader(application.classLoader))
def beans = bb.beans {
myService(org.springframework.remoting.caucho.HessianProxyFactoryBean) {
...
}
}
beans.registerBeans(application.mainContext)
This is pretty much what plugins do when they need to swap in new bean instances. You could also raise a JIRA issue for a nicer way of doing this.

Categories

Resources