javax.naming.NameNotFoundException 在 EJB 中重新启动后 windows
javax.naming.NameNotFoundException after restart windows in EJB
我正在按照在线指南学习 EJB。在引入 Singleton(@ConcurrencyManagement、@Lock 等)之前,一切都运行良好,通过 JUnit 测试进行测试。当我开始使用单例时,我发现在测试过程中,JNDI 给出了一个关于 ClassNotFoundException 的错误。我在互联网上搜索但没有解决。因此,我决定重新启动我的电脑,在之前关于以前工作方法的 junit 测试之后,不起作用!现在他们给我同样的错误:ClassNotFoundException。我用wildfly9
联合测试:
package it.html.progetto1.test;
import java.util.Properties;
import javax.naming.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import it.html.progetto1.ejb30.ConverterBeanRemote;
import it.html.progetto1.singleton.ejb32.NewsSingleton;
import it.html.progetto1.singleton.ejb32.NewsSingletonRemote;
import it.html.progetto1.stateful.ejb32.ShoppingCartRemote;
import junit.framework.TestCase;
public class IntegrationTestCase {
private static Context namingContext;
private static ConverterBeanRemote converteBeanRemote;
private static ShoppingCartRemote<String> shoppingCartRemote;
private static NewsSingleton<String> newsSingletonRemote;
private static final String CONVERTER_REMOTE_JNDI_NAME="/Progetto1Ear/ProgettoEjb1/ConverterBean!it.html.progetto1.ejb30.ConverterBeanRemote";
private static final String SHOPPING_CART_REMOTE_JNDI_NAME="/Progetto1Ear/ProgettoEjb1/ShoppingCartBean!it.html.progetto1.stateful.ejb32.ShoppingCartRemote";
private static final String SINGLETON_REMOTE_JNDI_NAME="/Progetto1Ear/ProgettoEjb1/NewsSingletonBean!it.html.progetto1.singleton.ejb32.NewsSingletonRemote";
@BeforeClass
public static void obtainProxyReferences() throws NamingException{
Properties jndiProperties = new Properties();
jndiProperties.put("jboss.naming.client.ejb.context", true);
jndiProperties.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
namingContext = new InitialContext(jndiProperties);
}
@Test
public void testConverter() throws NamingException{
converteBeanRemote = (ConverterBeanRemote)namingContext.lookup(CONVERTER_REMOTE_JNDI_NAME);
TestCase.assertEquals(converteBeanRemote.celsiusToFahrenheit(30.0f), 86.0f);
TestCase.assertEquals(converteBeanRemote.fahrenheitToCelsius(86.0f), 30.0f);
}
@SuppressWarnings("unchecked")
@Test
public void testShoppingCart() throws NamingException{
shoppingCartRemote = (ShoppingCartRemote<String>)namingContext.lookup(SHOPPING_CART_REMOTE_JNDI_NAME);
System.out.println("Aggiunta elemento 1");
shoppingCartRemote.addItem("Item1");
System.out.println("Aggiunta elemento 2");
shoppingCartRemote.addItem("Item2");
System.out.println("Lista elementi:");
for(String item : shoppingCartRemote.getItems()){
System.out.println(item);
}
System.out.println("Rimozione elemento 1");
shoppingCartRemote.removeItem("Item1");
System.out.println("Lista elementi:");
for(String item : shoppingCartRemote.getItems()){
System.out.println(item);
}
shoppingCartRemote.releaseShoppingCart();
}
@SuppressWarnings("unchecked")
@Test
public void testSingleton() throws NamingException{
newsSingletonRemote = (NewsSingletonRemote<String>)namingContext.lookup(SINGLETON_REMOTE_JNDI_NAME);
System.out.println("Aggiunta news 1");
newsSingletonRemote.addNews("News1");
System.out.println("Aggiunta news 2");
newsSingletonRemote.addNews("News2");
System.out.println("News :");
for(String news : newsSingletonRemote.getNews()){
System.out.println("News:"+news);
}
System.out.println("Rimozione news 1");
newsSingletonRemote.removeNews("News1");
System.out.println("News :");
for(String news : newsSingletonRemote.getNews()){
System.out.println("News:"+news);
}
}
@AfterClass
public static void tearDownClass() throws NamingException {
namingContext.close();
}
}
重新启动前,testConverter() 测试有效,但之后就不行了!!最初只有 testSingleton() 和 testShoppingCart 坏了。
在包it.html.progetto1.ejb30中我有以下类(重启后测试成功的方法)
Converter.java
package it.html.progetto1.ejb30;
public interface Converter {
float celsiusToFahrenheit(float temperature);
float fahrenheitToCelsius(float temperature);
}
ConverterBean.java
package it.html.progetto1.ejb30;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
/**
* Session Bean implementation class ConverterBean
*/
@Stateless
@LocalBean
public class ConverterBean implements ConverterBeanRemote, ConverterBeanLocal {
/**
* Default constructor.
*/
public ConverterBean() {
// TODO Auto-generated constructor stub
}
@Override
public float celsiusToFahrenheit(float temperature) {
return temperature * 1.8f + 32;
}
@Override
public float fahrenheitToCelsius(float temperature) {
return (temperature - 32) / 1.8f;
}
}
ConverterBeanLocal.java
package it.html.progetto1.ejb30;
import javax.ejb.Local;
@Local
public interface ConverterBeanLocal extends Converter{
}
ConverterBeanRemote.java
package it.html.progetto1.ejb30;
import javax.ejb.Remote;
@Remote
public interface ConverterBeanRemote extends Converter{
}
在包 it.html.progetto1.singleton.ejb32 单例 类 中,问题的开始(从未在测试中起作用):
NewsSingleton.java
package it.html.progetto1.singleton.ejb32;
import java.util.List;
public interface NewsSingleton<T> {
List<T> getNews();
void addNews(T news);
void removeNews(T news);
}
NewsSingletonBean.java
package it.html.progetto1.singleton.ejb32;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.ejb.AccessTimeout;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.ejb.Startup;
// @Singleton marca la classe come Session Bean Singleton
@Singleton
// @Startup fa in modo che il bean venga allocato in fase di deploy dal modulo Ejb
@Startup
// Container-Managed-Concurrency è un'astrazione semplificata del meccanismo di locking
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class NewsSingletonBean implements NewsSingletonLocal<String>, NewsSingletonRemote<String> {
private List<String> newsList;
public NewsSingletonBean(){
newsList=new ArrayList<String>();
}
// locca in fase di lettura, molti thread possono leggere contemporaneamente (acceso contemporaneo)
@Lock(LockType.READ)
/*
* invece di avere un thread che, dopo aver invocato un metodo con lock Read, si trova in attesa perché
* il singleton è in lock da parte di un altro thread, possiamo specificare un limite per l’attesa (in questo caso 15 secondi).
* Allo scadere del timeout, se il lock non è stato rilasciato si verifica un’eccezione.
*/
@AccessTimeout(unit=TimeUnit.SECONDS, value=15)
public List<String> getNews(){
return newsList;
}
// locca in fase di scrittura, e quindi un solo thread alla volta scrive e gli altri sono bloccati
@Lock(LockType.WRITE)
public void addNews(String news){
newsList.add(news);
}
@Lock(LockType.WRITE)
public void removeNews(String news){
newsList.remove(news);
}
}
NewsSingletonLocal.java
package it.html.progetto1.singleton.ejb32;
import javax.ejb.Local;
@Local
public interface NewsSingletonLocal<T> extends NewsSingleton<T> {}
NewsSingletonRemote.java
package it.html.progetto1.singleton.ejb32;
import javax.ejb.Remote;
@Remote
public interface NewsSingletonRemote<T> extends NewsSingleton<T> {}
跟踪:
javax.naming.NameNotFoundException: Progetto1Ear -- service jboss.naming.context.java.jboss.exported.Progetto1Ear
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)
at org.wildfly.naming.client.remote.RemoteServerTransport.handleLookup(RemoteServerTransport.java:200)
at org.wildfly.naming.client.remote.RemoteServerTransport.handleMessage(RemoteServerTransport.java:120)
at org.jboss.remoting3.remote.RemoteConnectionChannel.lambda$handleMessageData(RemoteConnectionChannel.java:430)
at org.jboss.remoting3.EndpointImpl$TrackingExecutor.lambda$execute[=21=](EndpointImpl.java:991)
at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
at java.lang.Thread.run(Unknown Source)
教训:检查一下你的服务器之前有没有异常!事实上,控制台说有两个同名的 localBean (ConverterBean),因此映射为同一个。更改 ConverterBean 名称(例如:CoverterBeanI)有效,所有方法均有效。
我正在按照在线指南学习 EJB。在引入 Singleton(@ConcurrencyManagement、@Lock 等)之前,一切都运行良好,通过 JUnit 测试进行测试。当我开始使用单例时,我发现在测试过程中,JNDI 给出了一个关于 ClassNotFoundException 的错误。我在互联网上搜索但没有解决。因此,我决定重新启动我的电脑,在之前关于以前工作方法的 junit 测试之后,不起作用!现在他们给我同样的错误:ClassNotFoundException。我用wildfly9
联合测试:
package it.html.progetto1.test;
import java.util.Properties;
import javax.naming.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import it.html.progetto1.ejb30.ConverterBeanRemote;
import it.html.progetto1.singleton.ejb32.NewsSingleton;
import it.html.progetto1.singleton.ejb32.NewsSingletonRemote;
import it.html.progetto1.stateful.ejb32.ShoppingCartRemote;
import junit.framework.TestCase;
public class IntegrationTestCase {
private static Context namingContext;
private static ConverterBeanRemote converteBeanRemote;
private static ShoppingCartRemote<String> shoppingCartRemote;
private static NewsSingleton<String> newsSingletonRemote;
private static final String CONVERTER_REMOTE_JNDI_NAME="/Progetto1Ear/ProgettoEjb1/ConverterBean!it.html.progetto1.ejb30.ConverterBeanRemote";
private static final String SHOPPING_CART_REMOTE_JNDI_NAME="/Progetto1Ear/ProgettoEjb1/ShoppingCartBean!it.html.progetto1.stateful.ejb32.ShoppingCartRemote";
private static final String SINGLETON_REMOTE_JNDI_NAME="/Progetto1Ear/ProgettoEjb1/NewsSingletonBean!it.html.progetto1.singleton.ejb32.NewsSingletonRemote";
@BeforeClass
public static void obtainProxyReferences() throws NamingException{
Properties jndiProperties = new Properties();
jndiProperties.put("jboss.naming.client.ejb.context", true);
jndiProperties.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
namingContext = new InitialContext(jndiProperties);
}
@Test
public void testConverter() throws NamingException{
converteBeanRemote = (ConverterBeanRemote)namingContext.lookup(CONVERTER_REMOTE_JNDI_NAME);
TestCase.assertEquals(converteBeanRemote.celsiusToFahrenheit(30.0f), 86.0f);
TestCase.assertEquals(converteBeanRemote.fahrenheitToCelsius(86.0f), 30.0f);
}
@SuppressWarnings("unchecked")
@Test
public void testShoppingCart() throws NamingException{
shoppingCartRemote = (ShoppingCartRemote<String>)namingContext.lookup(SHOPPING_CART_REMOTE_JNDI_NAME);
System.out.println("Aggiunta elemento 1");
shoppingCartRemote.addItem("Item1");
System.out.println("Aggiunta elemento 2");
shoppingCartRemote.addItem("Item2");
System.out.println("Lista elementi:");
for(String item : shoppingCartRemote.getItems()){
System.out.println(item);
}
System.out.println("Rimozione elemento 1");
shoppingCartRemote.removeItem("Item1");
System.out.println("Lista elementi:");
for(String item : shoppingCartRemote.getItems()){
System.out.println(item);
}
shoppingCartRemote.releaseShoppingCart();
}
@SuppressWarnings("unchecked")
@Test
public void testSingleton() throws NamingException{
newsSingletonRemote = (NewsSingletonRemote<String>)namingContext.lookup(SINGLETON_REMOTE_JNDI_NAME);
System.out.println("Aggiunta news 1");
newsSingletonRemote.addNews("News1");
System.out.println("Aggiunta news 2");
newsSingletonRemote.addNews("News2");
System.out.println("News :");
for(String news : newsSingletonRemote.getNews()){
System.out.println("News:"+news);
}
System.out.println("Rimozione news 1");
newsSingletonRemote.removeNews("News1");
System.out.println("News :");
for(String news : newsSingletonRemote.getNews()){
System.out.println("News:"+news);
}
}
@AfterClass
public static void tearDownClass() throws NamingException {
namingContext.close();
}
}
重新启动前,testConverter() 测试有效,但之后就不行了!!最初只有 testSingleton() 和 testShoppingCart 坏了。
在包it.html.progetto1.ejb30中我有以下类(重启后测试成功的方法)
Converter.java
package it.html.progetto1.ejb30;
public interface Converter {
float celsiusToFahrenheit(float temperature);
float fahrenheitToCelsius(float temperature);
}
ConverterBean.java
package it.html.progetto1.ejb30;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
/**
* Session Bean implementation class ConverterBean
*/
@Stateless
@LocalBean
public class ConverterBean implements ConverterBeanRemote, ConverterBeanLocal {
/**
* Default constructor.
*/
public ConverterBean() {
// TODO Auto-generated constructor stub
}
@Override
public float celsiusToFahrenheit(float temperature) {
return temperature * 1.8f + 32;
}
@Override
public float fahrenheitToCelsius(float temperature) {
return (temperature - 32) / 1.8f;
}
}
ConverterBeanLocal.java
package it.html.progetto1.ejb30;
import javax.ejb.Local;
@Local
public interface ConverterBeanLocal extends Converter{
}
ConverterBeanRemote.java
package it.html.progetto1.ejb30;
import javax.ejb.Remote;
@Remote
public interface ConverterBeanRemote extends Converter{
}
在包 it.html.progetto1.singleton.ejb32 单例 类 中,问题的开始(从未在测试中起作用):
NewsSingleton.java
package it.html.progetto1.singleton.ejb32;
import java.util.List;
public interface NewsSingleton<T> {
List<T> getNews();
void addNews(T news);
void removeNews(T news);
}
NewsSingletonBean.java
package it.html.progetto1.singleton.ejb32;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.ejb.AccessTimeout;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.ejb.Startup;
// @Singleton marca la classe come Session Bean Singleton
@Singleton
// @Startup fa in modo che il bean venga allocato in fase di deploy dal modulo Ejb
@Startup
// Container-Managed-Concurrency è un'astrazione semplificata del meccanismo di locking
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class NewsSingletonBean implements NewsSingletonLocal<String>, NewsSingletonRemote<String> {
private List<String> newsList;
public NewsSingletonBean(){
newsList=new ArrayList<String>();
}
// locca in fase di lettura, molti thread possono leggere contemporaneamente (acceso contemporaneo)
@Lock(LockType.READ)
/*
* invece di avere un thread che, dopo aver invocato un metodo con lock Read, si trova in attesa perché
* il singleton è in lock da parte di un altro thread, possiamo specificare un limite per l’attesa (in questo caso 15 secondi).
* Allo scadere del timeout, se il lock non è stato rilasciato si verifica un’eccezione.
*/
@AccessTimeout(unit=TimeUnit.SECONDS, value=15)
public List<String> getNews(){
return newsList;
}
// locca in fase di scrittura, e quindi un solo thread alla volta scrive e gli altri sono bloccati
@Lock(LockType.WRITE)
public void addNews(String news){
newsList.add(news);
}
@Lock(LockType.WRITE)
public void removeNews(String news){
newsList.remove(news);
}
}
NewsSingletonLocal.java
package it.html.progetto1.singleton.ejb32;
import javax.ejb.Local;
@Local
public interface NewsSingletonLocal<T> extends NewsSingleton<T> {}
NewsSingletonRemote.java
package it.html.progetto1.singleton.ejb32;
import javax.ejb.Remote;
@Remote
public interface NewsSingletonRemote<T> extends NewsSingleton<T> {}
跟踪:
javax.naming.NameNotFoundException: Progetto1Ear -- service jboss.naming.context.java.jboss.exported.Progetto1Ear
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)
at org.wildfly.naming.client.remote.RemoteServerTransport.handleLookup(RemoteServerTransport.java:200)
at org.wildfly.naming.client.remote.RemoteServerTransport.handleMessage(RemoteServerTransport.java:120)
at org.jboss.remoting3.remote.RemoteConnectionChannel.lambda$handleMessageData(RemoteConnectionChannel.java:430)
at org.jboss.remoting3.EndpointImpl$TrackingExecutor.lambda$execute[=21=](EndpointImpl.java:991)
at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
at java.lang.Thread.run(Unknown Source)
教训:检查一下你的服务器之前有没有异常!事实上,控制台说有两个同名的 localBean (ConverterBean),因此映射为同一个。更改 ConverterBean 名称(例如:CoverterBeanI)有效,所有方法均有效。