Error in initialising portlets on portal server - spring

I have a WebSphere Portal server that houses portlets. The portlets make remote calls to ejbs running on WAS. Both the portal server and the WAS are running on the local machine. The WAS starts up properly, but on trying to start up the portal server, it throws the following error:
nested exception is: java.lang.NullPointerException at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
On looking up the source in spring, I find the method definition:
public PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
InjectionMetadata metadata = findAutowiringMetadata(bean.getClass());
try {
metadata.injectMethods(bean, beanName, pvs);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Autowiring of methods failed", ex);
}
return pvs;
}
Since the error is in spring source code and not my code, how should I start debugging it?

Related

Stop hibernate from throwing sensitive data when an exception is thrown

I am new to hibernate and Im using JPA +Hibernate + Spring Boot for my applications. The logging is being handled using log4j2 . When my application is correctly throwing an exception (in my case DataIntegrityViolationException or EntityExistsException) Hibernate is logging the exception with stackTrace. Which is great as it helps debug. However it also logs application sensitive data which I do not want to be present in the logs. Is there a setting in the properties file that can be set or modified to prevent this from happening. I don’t want to create an appender for hibernate in my log4j2.xml and mask specific words in the final log.
You can add global exception in spring boot and you can log the exception message only follow this tutorial - https://www.studytonight.com/spring-boot/spring-boot-global-exception-handling
Or you can do some try catch for each call and throw exception with your customized message as below.
Try {
//call
} catch (Exception ex){
throws CustomizedRunTimeException(“yourMessage”)
}
The class look like below
public class CustomizedRunTimeException extends RunTimeException {
public CustomizedRunTimeException(String errorMessage) {
super(errorMessage);
}
}

How do I catch BeanCreationException in Apache Camel route initialization to allow my Spring application to start anyway?

I am using Apache Camel and I defined a route receiving inputs from 2 queues located on 2 distinct servers. I want ideally to consume from both server but I also want to be able to run the application when one of the 2 destinations is down.
Here's my route:
try {
from("mccdsJmsRequest1:queue:{{mccds.queues.in}}").to("direct:apolloMccdsRoute");
} catch (Exception e){
// debug line
System.out.println("Ms1 not reachable");
e.printStackTrace();
}
try {
from("mccdsJmsRequest2:queue:{{mccds.queues.in}}").to("direct:apolloMccdsRoute");
} catch (Exception e){
// debug line
System.out.println("Ms2 not reachable");
e.printStackTrace();
}
from("direct:apolloMccdsRoute").routeId("apolloMCCDSRoute")
// Main route starts here...
I declare my beans here:
#Bean
public JndiObjectFactoryBean mccdsJmsConnectionFactory1() {
JndiObjectFactoryBean cf = new JndiObjectFactoryBean();
cf.setJndiEnvironment(prodMccdsJndiProperties.getJndi1());
cf.setJndiName(jndiName1);
return cf;
}
#Bean
public JndiObjectFactoryBean mccdsJmsConnectionFactory2(){
JndiObjectFactoryBean cf = new JndiObjectFactoryBean();
cf.setJndiEnvironment(prodMccdsJndiProperties.getJndi2());
cf.setJndiName(jndiName2);
return cf;
}
#Inject
private CamelContext camelContext;
#Bean
public JmsComponent mccdsJmsRequest1() {
JmsComponent ac = new JmsComponent(camelContext);
ac.setConnectionFactory((ConnectionFactory) mccdsJmsConnectionFactory1().getObject());
ac.setConcurrentConsumers(5);
return ac;
}
#Bean
public JmsComponent mccdsJmsRequest2(){
JmsComponent ac = new JmsComponent(camelContext);
ac.setConnectionFactory((ConnectionFactory) mccdsJmsConnectionFactory2().getObject());
ac.setConcurrentConsumers(5);
return ac;
}
If one of the connection factory is not reachable the application doesn't start.
I would like to catch an ignore the exception:
o.s.b.f.s.DefaultListableBeanFactory : Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mccdsJmsConnectionFactory2' defined in class path resource [ca/bell/it/spa/uim/apollo/maximo/config/mccds/ProdMccdsJmsConfiguration.class]: Invocation of init method failed; nested exception is javax.naming.CommunicationException [Root exception is java.net.ConnectException: t3://someTestServerIP: Destination unreachable; nested exception is:
java.net.ConnectException: Connection refused (Connection refused); No available router to destination]
Try to set lookupOnStartup to false. (Add cf.setLookupOnStartup(false) to the mccdsJmsConnectionFactory1 and mccdsJmsConnectionFactory2 bean definitions.)
Also check this thread: http://forum.spring.io/forum/spring-projects/batch/95620-jndi-lookup
That won't work because your try/catch blocks are never executed during the execution of your route. In your code, you'd only get an exception during Camel's initial execution of this code upon application startup when it's building the route, when it doesn't care if your queues even exist or not.
Instead you need to determine when an error occurs in your route when it's executed after being constructed. There are a number of ways to do this, but a good start might be to look at Camel's Exception Clause
And alternative and perhaps even better choice is to make use of Camel's Load Balancer . It allows you to try any number of endpoints and failover to the next should one fail with an exception.

Handling code/configuration error from entry action classes in spring state machine

I am using a state machine builder to build state machine in my app.
Also the application has Action classes which implements org.springframework.statemachine.action.Action.
These Action classes are for executing entry actions for each stages.
If any exception is thrown from these Action classes, ie from execute(StateContext paramStateContext) method, I wanted to catch that exception and send an event(Terminated) and drive the state machine to End state, after updating the db with error details.
I tried to use state machine listener by overriding stateMachineError(StateMachine stateMachine, Exception e) method. But unfortunately this is not working.
Any other spring state machine component to catch exceptions, before I go to wrap the entire code in Action classes with try catch, and inside catch block sending the Terminated event so that state machine would navigate End state.
Here is the builder Iam using.
Builder<String, String> builder = StateMachineBuilder
.<String, String> builder();
builder.configureConfiguration()
.withConfiguration()
.autoStartup(false)
.listener(listener())
.beanFactory(
this.applicationContext.getAutowireCapableBeanFactory());
private StateMachineListener<String, String> listener() {
return new StateMachineListenerAdapter<String, String>() {
#Override
public void stateChanged(
org.springframework.statemachine.state.State<String, String> from,
org.springframework.statemachine.state.State<String, String> to) {
LOGGER.debug("State change to " + to.getId());
}
#Override
public void stateMachineError(
StateMachine<String, String> stateMachine, Exception e) {
e.printStackTrace();
LOGGER.debug("Ah... I am not getting executed when exception occurs from entry actions");
LOGGER.debug("Error occured from " + stateMachine.getState()
+ "and the error is" + e.toString());
}
};
}
Iam using 1.1.0.RELEASE version of spring-statemachine-core
You're correct, neither stateMachineError or a method annotated with #onStateMachineError are executed on an error. This is addressed in version 1.2, which is at milestone 1 right now. They introduced errorAction which is executed with the state machine context:
User can always catch exceptions manually but with actions defined for transitions it is possible to define error action which is called if exception is raised. Exception is then available from a StateContext passed to that action.
All that's needed is to specify an error action along with the desired action when defining a transition in the state machine configuration class. From the example in the documentation:
public void configure(StateMachineTransitionConfigurer<States, Events> transitions)
throws Exception {
transitions
.withExternal()
.source(States.S1)
.target(States.S2)
.event(Events.E1)
.action(action(), errorAction());
}
A detailed discussion of this can be found in Spring Statemachine issue #240.

Remote JNDI access to a single resource in TomEE

I'm trying to set an object into JNDI and then get remote access to it. I'm using TomEE 1.6.0. I'm setting a sinple string using an servlet like this:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
try
{
Context ctx=new InitialContext();
ctx.bind("myKey","MY STRING");
}
catch(NamingException ex)
{
ex.printStackTrace();
}
}
After running and invoke this servlet, I try to get remote access through JNDI using this standalone main.
public static void main(String[] args) throws Exception
{
Context ctx = getContext();
String nom = (String)ctx.lookup("java:/comp/env/nombre");
System.out.println(nom);
}
private static Context getContext() throws Exception
{
Hashtable<String,String> t = new Hashtable<>();
t.put("java.naming.factory.initial","org.apache.openejb.client.RemoteInitialContextFactory");
t.put("java.naming.provider.url","http://127.0.0.1:8080/tomee/ejb");
return new InitialContext(t);
}
But it throws an NameNotFoundException like this:
Exception in thread "main" javax.naming.NameNotFoundException: /comp/env/nombre does not exist in the system. Check that the app was successfully deployed.
at org.apache.openejb.client.JNDIContext.lookup(JNDIContext.java:319)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at demo.TestJNDI.main(TestJNDI.java:13)
So, my question are two:
1 - How can I know the default JNDI name which is using TomEE to publish this string?
2 - How can I set this string into any XML file instead the servlet?
Thanks!
Not sure what you expect to do but remote context is mainly an ejb/resource one. comp/env is clearly local to the application

getting remote exception while calling an ejb3 method

method(){
try{
some code..
}
catch(Exception e)
{
throw new userDefineException();
}
}
// while calling the above method from the java client I am gettine remote exception but i am expecting to get UserdefineException.
EJB container will wrap undeclared (system) exceptions in RemoteException (or EJBException for local views). To avoid this, you should either:
Change UserDefineException to extend Exception rather than RuntimeException, and add UserDefineException to the throws clause of the remote interface.
Annotation UserDefineException with #ApplicationException, or specify it as <application-exception>com.example.UserDefineException</application-exception> in ejb-jar.xml.

Resources