0

I'm trying to carve out a legacy feature to put it in a standalone Spring Boot Application. I had better success adding a DataSource Resource programmatically, but currently trying to add a JMS resource in a similar fashion, but keep getting hit with

javax.naming.NamingException: Could not create resource instance

My code:

TomcatConfig.java
[...]
public TomcatServletWebServerFactory servletContainer() {
        return new TomcatServletWebServerFactory() {
            @Override
            protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
                tomcat.enableNaming();
                return super.getTomcatWebServer(tomcat);
            }
            @Override
            protected void postProcessContext(Context context) {
                // JMS ConnectionFactory
                ContextResource jmsConnection = new ContextResource();
                jmsConnection.setName("jms/jmsConnection");
                jmsConnection.setType("javax.jms.ConnectionFactory");
                jmsConnection.setProperty("resourceAdapter", "openejb/Resource/JmsResourceAdapter");

                context.getNamingResources().addResource(jmsConnection);
            }
        };
    }

@Bean(destroyMethod="")
    public ConnectionFactory jndiConnectionFactory() throws IllegalArgumentException, NamingException {
        JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
        bean.setJndiName("java:comp/env/jms/jmsConnection");
        bean.setProxyInterface(ConnectionFactory.class);
        bean.setLookupOnStartup(true);
        bean.afterPropertiesSet();                   // NamingException here!!
        return (ConnectionFactory) bean.getObject();
    }

Any guidance is appreciated here; I'm quite new at this modernizing topic. Is this because Tomcat does not support EJB somehow?

I tried creating a ContextResource programmatically, but having a hard time getting the lookup/binding to stick to it (somehow can't instantiate the jndiConnectionFactory bean. The stacktrace is not very helpful:

NamingException during context initialization: javax.naming.NamingException: Could not create resource instance
javax.naming.NamingException: Could not create resource instance
    at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:98)
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:864)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:158)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:850)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:172)
    at com......CustomContextListener.contextInitialized(CustomContextListener.java:25)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4462)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4914)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:171)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1332)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1322)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:866)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:794)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:171)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1332)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1322)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:866)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:248)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:171)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:433)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:171)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:921)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:171)
    at org.apache.catalina.startup.Tomcat.start(Tomcat.java:489)
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:123)
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:104)
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:481)
    at com.............configuration.TomcatConfiguration$1.getTomcatWebServer(TomcatConfiguration.java:31)
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:211)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:184)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:162)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:585)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:409)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1289)
4
  • 1
    There must be more of the stack trace than that. Another 'caused by'?
    – user207421
    Commented Jun 18 at 1:53
  • I will post the full stack trace, but that is the bottomest one...
    – onogram
    Commented Jun 18 at 1:55
  • (1). Do not put EJB writing into Spring Boot (2). Spring Boot supports JMS. Please find Spring Boot docs JMS, the URL is here: "docs.spring.io/spring-framework/docs/3.2.x/…" (3) Check, whether you use Spring Boot 2 or Spring Boot 3, the name of the package will be different (javax vs jakarta)
    – life888888
    Commented Jun 18 at 9:01
  • Am well aware that SB does not support EJB; not out of the box at least. From what I read, you can bring in the JavaEE libraries in as you see fit. My conundrum is that it's a legacy JavaEE monolith, and I'm only trying to carve out one feature out of it. So trying to reuse as many of the existing classes as I can. Just wondering what I'm missing from my current configuration.
    – onogram
    Commented Jun 18 at 13:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.