Spring Boot – Tomcat JNDI – 在此上下文中未绑定错误

huangapple 未分类评论65阅读模式
英文:

Spring Boot - Tomcat JNDI - Not bound in this context error

问题

以下是翻译好的部分:

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;

@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/");
	}

	@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"
}
英文:

As part of migrating our Spring MVC scheduler project to Spring boot 2.0.6 I am getting below error while getting connection from JNDI to connect to JMS broker.

After the JNDI is created we are initiating JMS connection to third party application using the JNDI. I am stuck with this, can someone help?

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(&quot;${request.queue.name}&quot;)
	private String requestQueueName;
	@Value(&quot;${request.queue.physical.name}&quot;)
	private String physicalRequestQueue;
	@Value(&quot;${response.queue.name}&quot;)
	private String responseQueueName;
	@Value(&quot;${response.queue.physical.name}&quot;)
	private String physicalResponseQueue;
	@Value(&quot;${broker.queue.name}&quot;)
	private String brokerQueue;
	@Value(&quot;${broker.url}&quot;)
	private String brokerUrl;
	@Value(&quot;${broker.username}&quot;)
	private String brokerUserName;
	@Value(&quot;${broker.password}&quot;)
	private String brokerPassword;


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

	/**
	 * 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(&quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSSZ&quot;);
		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&lt;ContextResource&gt; resources = new ArrayList&lt;&gt;();
				buildGetCurrentFlightRequestQ(resources);
				buildGetCurrentFlightResponseQ(resources);
				buildBrokerDetails(resources);
				resources.forEach(resource -&gt; context.getNamingResources().addResource(resource));
			}

		};
	}


	private void buildGetCurrentFlightRequestQ(List&lt;ContextResource&gt; 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&lt;ContextResource&gt; 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&lt;ContextResource&gt; 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);
	}

}

Error

{&quot;@timestamp&quot;:&quot;2020-03-24T13:35:00.579Z&quot;,&quot;message&quot;:&quot;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&quot;,&quot;logger_name&quot;:&quot;com.xxx.yyy.connector.service.JmsSystemConnector&quot;,&quot;thread_name&quot;:&quot;scheduling-1&quot;,&quot;level&quot;:&quot;ERROR&quot;,&quot;context&quot;:&quot;default&quot;,&quot;mdc&quot;:{&quot;errorCode&quot;:&quot;00000&quot;,&quot;stackTrace&quot;:&quot;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

huangapple
  • 本文由 发表于 2020年4月10日 20:20:00
  • 转载请务必保留本文链接:https://java.coder-hub.com/61140106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定