- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在我的程序中实现简单的缓存。因此,为此我正在尝试在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但我收到错误如下:
最佳答案
我让你的项目在我的环境中运行(或者至少在 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/
我一直在尝试在我的代码中使用 Jar 文件作为库,并且它编译得很好。但是,在运行时,我不断收到 NoClassDefFoundError信息。为什么会这样?我也在编译路径和运行时路径中包含了 Jar
关于Apache-Kafka messaging queue . 我已经从 Kafka 下载页面下载了 Apache Kafka。我已将其提取到 /opt/apache/installed/kafka
我正在尝试使用 Apache DefaultHttpClient 来执行 JSON POST 请求,当我尝试实例化它时它给我一个 NoClassDefFound 错误。 HttpClient clie
当我在模拟器(Nexus One API 22)上测试我的应用程序时,它运行顺利,没有失败。然而,当我在自己的个人手机(三星 Galaxy S5,Android 版本 5.0)上测试该应用程序时,它崩
我需要在python中使用java代码来减少,所以我选择了Jython。一段时间后,我设法弄清楚了如何运行我的代码,但我遇到了最奇怪的事情。当我写作时 from vohmm.corpus import
这是我的mybatis配置。这是我的pom.xml。。当我运行项目时,它显示了错误的原因:org/mybatis/spring/mapper/MapperScannerConfigurer.有没有人能
所以我正在尝试构建一个简单的gradle应用,当我运行它时, geb.ConfigurationLoader$UnableToLoadException: Unable to load configu
假设我有一个主类应用程序,它使用 URLClassLoader 加载子目录 plugins 中的所有 jar: public class App(){ public static void m
我在尝试运行 Netbeans (7.2) 时遇到一个反复出现的错误,上次遇到它时,我发现某个地方可以将所有文件移动到一个新项目。这可能会奏效,但我的项目的规模让这很麻烦。这是踪迹... Except
这个问题已经有答案了: Including all the jars in a directory within the Java classpath (25 个回答) 已关闭 6 年前。 我得到一个
在这里,我正在下载网页源代码,然后将其存储在文本文件中。然后我读取该文件并将其与正则表达式匹配以搜索特定字符串。 没有编译器错误。 Exception in thread "main" java.la
我正在一个“大”的 Maven/Java 项目中工作,无法理解运行应用程序时遇到的错误(它编译正常)。我得到的错误代码是: java.lang.NoClassDefFoundError: Could
对于学校的作业,我需要创建一个类 Blender 来实现一些预定义的东西。我收到了一个 JAR 文件 imagecompositor.jar,它可以完成所有操作并使用 Blender 类。 JAR 文
我遇到了一个问题,即抛出 NoClasDefFoundError。这让我感到困惑,因为我正在使用接口(interface),并且没有类定义应该可用。我已经阅读了一些指向类路径的帖子,但我不认为这是这里
我正在使用 hibernate,在使用 hibernate Connection 时出现如下错误 java.lang.NoClassDefFoundError: Could not initializ
我有一个使用 SubVersion 的 Android 项目。我使用 Subclipse 将项目导入我的 Eclipse Wordspace。 现在我有一个问题: java.lang.NoClass
我需要编译一个外部 java 文件(比如 a.java)。这是我为此编写的代码。 (字符串路径包含java和class文件的路径) command[0] = "javac"; comm
我正在尝试运行一个基本的 Hibernate 程序。当我运行它时,出现以下错误 java.lang.NoClassDefFoundError: org/jboss/logging/BasicLogge
标题:Eclipse插件开发由于java.lang.NoClassDefFoundError无法实例化类: 试图构建一个 eclipse 插件,但遇到一些运行时错误.. 我知道这是由于代码所依赖的类文
我是新手,我无法让它工作......:/ 我的 build.sbt: val apacheDeps = Seq( "commons-validator" % "commons-validator"
我是一名优秀的程序员,十分优秀!