gpt4 book ai didi

java - 错误 : java. lang.NoClassDefFoundError: org/apache/commons/collections/map/LRUMap

转载 作者:行者123 更新时间:2023-12-02 09:34:28 27 4
gpt4 key购买 nike

我正在尝试在我的程序中实现简单的缓存。因此,为此我正在尝试在java中使用Restful Web Services。我没有使用spring或任何类型的框架,只是基于maven的项目。

我的代码是:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.ashwin</groupId>
<artifactId>JaxMaven11</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>JaxMaven11</name>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

影响缓存的API:

package com.ashwin.jaxmaven11;

import com.ashwin.jaxmaven11.cache.CrunchifyInMemoryCacheTest;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.core.MediaType;

/**
* REST Web Service
*
* @author AshwinPC
*/
@Path(value="contact")
public class ContactAPI {


@GET
public String index(){

CrunchifyInMemoryCacheTest crunchifyCache = new CrunchifyInMemoryCacheTest();

System.out.println("\n\n==========Test1: crunchifyTestAddRemoveObjects ==========");
crunchifyCache.crunchifyTestAddRemoveObjects();
System.out.println("\n\n==========Test2: crunchifyTestExpiredCacheObjects ==========");
crunchifyCache.crunchifyTestAddRemoveObjects();
System.out.println("\n\n==========Test3: crunchifyTestObjectsCleanupTime ==========");
crunchifyCache.crunchifyTestAddRemoveObjects();

return "hello";
}

}

CrunchifyMemoryClass.java

package com.ashwin.jaxmaven11.cache;
import java.util.ArrayList;
import org.apache.commons.collections.MapIterator;
import org.apache.commons.collections.map.LRUMap;


public class CrunchifyInMemoryCache<K, T> {
private long timeToLive;
private LRUMap crunchifyCacheMap;

protected class CrunchifyCacheObject {
public long lastAccessed = System.currentTimeMillis();
public T value;

protected CrunchifyCacheObject(T value) {
this.value = value;
}
}

public CrunchifyInMemoryCache(long crunchifyTimeToLive, final long crunchifyTimerInterval, int maxItems) {
this.timeToLive = crunchifyTimeToLive * 1000;

crunchifyCacheMap = new LRUMap(maxItems);

if (timeToLive > 0 && crunchifyTimerInterval > 0) {

Thread t = new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(crunchifyTimerInterval * 1000);
} catch (InterruptedException ex) {
}
cleanup();
}
}
});

t.setDaemon(true);
t.start();
}
}

public void put(K key, T value) {
synchronized (crunchifyCacheMap) {
crunchifyCacheMap.put(key, new CrunchifyCacheObject(value));
}
}

@SuppressWarnings("unchecked")
public T get(K key) {
synchronized (crunchifyCacheMap) {
CrunchifyCacheObject c = (CrunchifyCacheObject) crunchifyCacheMap.get(key);

if (c == null)
return null;
else {
c.lastAccessed = System.currentTimeMillis();
return c.value;
}
}
}

public void remove(K key) {
synchronized (crunchifyCacheMap) {
crunchifyCacheMap.remove(key);
}
}

public int size() {
synchronized (crunchifyCacheMap) {
return crunchifyCacheMap.size();
}
}

@SuppressWarnings("unchecked")
public void cleanup() {

long now = System.currentTimeMillis();
ArrayList<K> deleteKey = null;

synchronized (crunchifyCacheMap) {
MapIterator itr = crunchifyCacheMap.mapIterator();

deleteKey = new ArrayList<K>((crunchifyCacheMap.size() / 2) + 1);
K key = null;
CrunchifyCacheObject c = null;

while (itr.hasNext()) {
key = (K) itr.next();
c = (CrunchifyCacheObject) itr.getValue();

if (c != null && (now > (timeToLive + c.lastAccessed))) {
deleteKey.add(key);
}
}
}

for (K key : deleteKey) {
synchronized (crunchifyCacheMap) {
crunchifyCacheMap.remove(key);
}

Thread.yield();
}
}
}

CruncifyInmemeoryTest.java

package com.ashwin.jaxmaven11.cache;
public class CrunchifyInMemoryCacheTest {


public void crunchifyTestAddRemoveObjects() {

// Test with crunchifyTimeToLive = 200 seconds
// crunchifyTimerInterval = 500 seconds
// maxItems = 6
CrunchifyInMemoryCache<String, String> cache = new CrunchifyInMemoryCache<String, String>(200, 500, 6);

cache.put("eBay", "eBay");
cache.put("Paypal", "Paypal");
cache.put("Google", "Google");
cache.put("Microsoft", "Microsoft");
cache.put("IBM", "IBM");
cache.put("Facebook", "Facebook");

System.out.println("6 Cache Object Added.. cache.size(): " + cache.size());
cache.remove("IBM");
System.out.println("One object removed.. cache.size(): " + cache.size());

cache.put("Twitter", "Twitter");
cache.put("SAP", "SAP");
System.out.println("Two objects Added but reached maxItems.. cache.size(): " + cache.size());

}

private void crunchifyTestExpiredCacheObjects() throws InterruptedException {

// Test with crunchifyTimeToLive = 1 second
// crunchifyTimerInterval = 1 second
// maxItems = 10
CrunchifyInMemoryCache<String, String> cache = new CrunchifyInMemoryCache<String, String>(1, 1, 10);

cache.put("eBay", "eBay");
cache.put("Paypal", "Paypal");
// Adding 3 seconds sleep.. Both above objects will be removed from
// Cache because of timeToLiveInSeconds value
Thread.sleep(3000);

System.out.println("Two objects are added but reached timeToLive. cache.size(): " + cache.size());

}

private void crunchifyTestObjectsCleanupTime() throws InterruptedException {
int size = 500000;

// Test with timeToLiveInSeconds = 100 seconds
// timerIntervalInSeconds = 100 seconds
// maxItems = 500000

CrunchifyInMemoryCache<String, String> cache = new CrunchifyInMemoryCache<String, String>(100, 100, 500000);

for (int i = 0; i < size; i++) {
String value = Integer.toString(i);
cache.put(value, value);
}

Thread.sleep(200);

long start = System.currentTimeMillis();
cache.cleanup();
double finish = (double) (System.currentTimeMillis() - start) / 1000.0;

System.out.println("Cleanup times for " + size + " objects are " + finish + " s");

}
}

我尝试访问网址:http://localhost:8080/JaxMaven11/api/contact但我收到错误如下:

enter image description here

最佳答案

我让你的项目在我的环境中运行(或者至少在 Chrome 上显示“hello”)

我假设我从未遇到过 505 错误,即使在我进行修改之前也是如此。无论如何,我添加了以下类来配置 servlet(否则我根本没有得到响应):

应用程序.java

package com.ashwin.jaxmaven11;

import javax.ws.rs.ApplicationPath;

@ApplicationPath("/api")
public class Application extends javax.ws.rs.core.Application {
}

然后我向该方法添加了 @Produce 注释。如果没有它,我无法在浏览器上看到“hello”,但它会显示 ERR_INVALID_SIGNED_EXCHANGE。 (尽管如此,它与 Postman 一起工作正常)

ContactAPI.java

[...]
@Path("/contact")
public class ContactAPI {

@GET
@Produces("text/html")
public String index(){
[...]
}

}

PS:我部署在Wildfly 11上,使用java 1.7编译。

关于java - 错误 : java. lang.NoClassDefFoundError: org/apache/commons/collections/map/LRUMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57660342/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com