Spring Boot - Tomcat JNDI - ошибка не связана с этим контекстом - PullRequest
0 голосов
/ 10 апреля 2020

Как часть миграции нашего проекта планировщика Spring MVC на Spring boot 2.0.6, я получаю ошибку ниже, когда получаю соединение от JNDI для соединения с JMS-брокером.

После создания JNDI мы инициируем соединение JMS со сторонним приложением, используя JNDI. Я застрял с этим, кто-то может помочь?

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.catalina.Context;
import org.apache.tomcat.util.descriptor.web.ContextResource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import com.xxx.yyy.api.service.constant.ServiceConstant;;

/**
 * This is the BASE API configuration file. Contains configuration which can be
 * used across the other API
 * 
 *
 */
@Configuration
@EnableAspectJAutoProxy
public class ApiConfiguration implements WebMvcConfigurer {
    @Value("${request.queue.name}")
    private String requestQueueName;
    @Value("${request.queue.physical.name}")
    private String physicalRequestQueue;
    @Value("${response.queue.name}")
    private String responseQueueName;
    @Value("${response.queue.physical.name}")
    private String physicalResponseQueue;
    @Value("${broker.queue.name}")
    private String brokerQueue;
    @Value("${broker.url}")
    private String brokerUrl;
    @Value("${broker.username}")
    private String brokerUserName;
    @Value("${broker.password}")
    private String brokerPassword;


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/docs/**").addResourceLocations("/docs/");
    }

    /**
     * Gets the null and empty filtered object mapper.
     *
     * @return the null and empty filtered object mapper
     */
    @Bean
    public ObjectMapper getNullAndEmptyFilteredObjectMapper() {

        ObjectMapper objectMapper = new ObjectMapper();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        objectMapper.setDateFormat(dateFormat);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        objectMapper.writerWithDefaultPrettyPrinter();
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.registerModule(new JodaModule());

        return objectMapper;
    }

    @Bean
    public TomcatServletWebServerFactory tomcatFactory() {
        return new TomcatServletWebServerFactory() {

            @Override
            protected TomcatWebServer getTomcatWebServer(org.apache.catalina.startup.Tomcat tomcat) {
                tomcat.enableNaming();
                return super.getTomcatWebServer(tomcat);
            }

            @Override
            protected void postProcessContext(Context context) {
                List<ContextResource> resources = new ArrayList<>();
                buildGetCurrentFlightRequestQ(resources);
                buildGetCurrentFlightResponseQ(resources);
                buildBrokerDetails(resources);
                resources.forEach(resource -> context.getNamingResources().addResource(resource));
            }

        };
    }


    private void buildGetCurrentFlightRequestQ(List<ContextResource> resources) {
        ContextResource resource = new ContextResource();
        resource.setName(requestQueueName);
        resource.setAuth(ServiceConstant.AUTH);
        resource.setType(ServiceConstant.CONTEXT);
        resource.setProperty(ServiceConstant.JNDI_NAME, requestQueueName);
        resource.setProperty(ServiceConstant.FACTORY, ServiceConstant.FACTORY_NAME);
        resource.setProperty(ServiceConstant.PHYSICAL_NAME, physicalRequestQueue);
        resources.add(resource);
    }

    private void buildGetCurrentFlightResponseQ(List<ContextResource> resources) {
        ContextResource resource = new ContextResource();
        resource.setName(responseQueueName);
        resource.setAuth(ServiceConstant.AUTH);
        resource.setType(ServiceConstant.CONTEXT);
        resource.setProperty(ServiceConstant.JNDI_NAME, responseQueueName);
        resource.setProperty(ServiceConstant.FACTORY, ServiceConstant.FACTORY_NAME);
        resource.setProperty(ServiceConstant.PHYSICAL_NAME, physicalResponseQueue);
        resources.add(resource);
    }

    private void buildBrokerDetails(List<ContextResource> resources) {
        ContextResource resource = new ContextResource();
        resource.setName(brokerQueue);
        resource.setAuth(ServiceConstant.AUTH);
        resource.setDescription(ServiceConstant.DESCRIPTION);
        resource.setType(ServiceConstant.BROKER_TYPE);
        resource.setProperty(ServiceConstant.JNDI_NAME, brokerQueue);
        resource.setProperty(ServiceConstant.FACTORY, ServiceConstant.FACTORY_NAME);
        resource.setProperty(ServiceConstant.URL, brokerUrl);
        resource.setProperty(ServiceConstant.USER_NAME, brokerUserName);
        resource.setProperty(ServiceConstant.PASSWORD, brokerPassword);
        resource.setProperty(ServiceConstant.FACTORY_CLASS, ServiceConstant.FACTORY_CLASS_VALUE);
        resources.add(resource);
    }

}

Ошибка

{"@timestamp":"2020-03-24T13:35:00.579Z","message":"Exception occured while sending message to SEIP/SAIS/Email/Paymentnotification Name [java:comp/env/jms/seipPilBrokerCF] is not bound in this Context. Unable to find [java:comp].#unexpected# java:comp/env/jms/seipPilBrokerCF java:comp/env/jms/seipWifiReportingMessageQueue  #SystemConnector#instance1","logger_name":"com.xxx.yyy.connector.service.JmsSystemConnector","thread_name":"scheduling-1","level":"ERROR","context":"default","mdc":{"errorCode":"00000","stackTrace":"Name [java:comp/env/jms/seipPilBrokerCF] is not bound in this Context. Unable to find [java:comp].javax.naming.NameNotFoundException: Name [java:comp/env/jms/seipPilBrokerCF] is not bound in this Context. Unable to find [java:comp].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:817)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at org.springframework.jndi.JndiTemplate.lambda$lookup$0(JndiTemplate.java:156)
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:91)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:156)
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
    at com.xxx.yyy.connector.factory.JmsTemplateFactory.getConnectionFactoryFromJndi(JmsTemplateFactory.java:109)
    at com.xxx.yyy.connector.factory.JmsTemplateFactory.getConnectionFactory(JmsTemplateFactory.java:81)
    at com.xxx.yyy.connector.factory.JmsTemplateFactory.createAndStoreJmsTemplate(JmsTemplateFactory.java:70)
    at com.xxx.yyy.connector.factory.JmsTemplateFactory.getJmsTemplate(JmsTemplateFactory.java:58)
    at com.xxx.yyy.connector.service.JmsSystemConnector.send(JmsSystemConnector.java:157)
    at com.xxx.yyy.handler.repository.SeipRepository.send(SeipRepository.java:24)
    at com.xxx.yyy.handler.repository.SeipRepository$$FastClassBySpringCGLI
...