我在我的应用程序中使用ScheduledExecutorService.我需要在某个Utility类中不时使用它来运行计划的线程.
在静态字段中保存ScheduledExecutorService是一个好的设计吗?在这种情况下是否必须调用ScheduledExecutorService.shutdown()?如果我不调用关机会有什么风险?
这就是我的想法:
private static ScheduledExecutorService exec = Executors.newScheduledThreadPool(5);
public void scheduleTask(String name) {
Future<?> future = futuresMapping.get(name);
if(future!=null && !future.isDone())
future.cancel(true);
//execute once
Future<?> f = scheduledExecutor.schedule(new MyTask()), 1, TimeUnit.MINUTES);
futuresMapping.put(name, f);
}
Run Code Online (Sandbox Code Playgroud)
谢谢
可能重复:
使用JPA指定索引(非唯一键)
有没有办法在enitity列上定义索引,以提高搜索性能?我看到hibernate给出了@Index和@IndexColumn,但我正在寻找JPA方法来做到这一点.
谢谢
这是我的实体的一个例子,我需要索引一个名称列
@Entity
@Table(name = "MY_TABLE")
public class MyEntity {
private long id;
private String name;
private String sourceInfo;
...
Run Code Online (Sandbox Code Playgroud) 我需要构建一个类似的日期格式dd/MM/yyyy
.它几乎就像DateFormat.SHORT
,但包含4年的数字.
我尝试用它来实现它
new SimpleDateFormat("dd//MM/yyyy", locale).format(date);
Run Code Online (Sandbox Code Playgroud)
但是对于美国语言环境,格式错误.
是否有一种通用的方法来格式化基于区域设置更改模式的日期?
谢谢
我可以写一个ant脚本,将更新的类文件复制到web-inf\classes
tomcat中.但是,如何告诉Tomcat 7.0自动从web-inf/classes
目录中获取更改的类文件?
我尝试autoDeploy="true"
在tomcat主机配置中设置,server.xml
但是当检测到更改时,Tomcat会话被销毁.我可以轻松地在Intellij中替换eclipse tomcat插件
我从JavaScript客户端(在机器A上运行)到Web服务器(在机器B上运行)进行AJAX调用.客户端尝试访问由RESTful Web服务(Jersey)公开的URL,并且它被阻止并显示错误:
Access-Control-Allow-Origin不允许使用origin http:// localhost /
在服务器中,我添加了2个标头参数,允许访问任何客户端.但它没有帮助:
@Context
private HttpServletResponse servlerResponse;
@POST
@Path("testme")
public void test(){
servlerResponse.addHeader("Access-Control-Allow-Origin", "*");
servlerResponse.addHeader("Access-Control-Allow-Credentials", "true");
}
Run Code Online (Sandbox Code Playgroud)
相同的头文件适用于JSP:
<%
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Credentials", "true");
%>
<html>
<head><title>test jsp</title></head>
<body>
test
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
谢谢
PS客户端部分是:
$.ajax({
type: "POST",
url: "http://localhost:8080/login/testme",
dataType: 'json',
success: onLoginSuccess,
error: onLoginError
});
Run Code Online (Sandbox Code Playgroud) 我ScheduledExecutorService
用来运行预定的线程.
我实现ServletContextListener.contextDestroyed
并调用了ScheduledExecutorService.shutdownNow
和awaitTermination
.
这是一个例子:
@Override
public void contextDestroyed(ServletContextEvent servletcontextevent) {
pool.shutdownNow(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(50, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
Run Code Online (Sandbox Code Playgroud)
不过,我从Tomcat 7收到以下错误:
严重:Web应用程序[/ servlet]似乎已经启动了一个名为[Timer-0]的线程,但未能将其停止.这很可能造成内存泄漏.
这个日志可以被忽略吗?或者我做错了什么?
谢谢
我有默认的 Maven 结构:
main
--java
--resources
--webapp
Run Code Online (Sandbox Code Playgroud)
我看到每个都mvn compile
复制资源,即使它们没有改变。我应该怎么做才能使构建副本仅更改文件?
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
...
</dependencies>
<properties>
<maven.resources.overwrite>false</maven.resources.overwrite>
</properties>
Run Code Online (Sandbox Code Playgroud)
这是调试模式下的输出:
[INFO] Using 'cp1252' encoding to copy filtered resources.
[DEBUG] resource with targetPath null
directory C:\core\src\main\resources
excludes []
includes []
[DEBUG] ignoreDelta true
[INFO] Copying 190 resources
[DEBUG] file batch.bat has a filtered file extension
[DEBUG] copy C:\core\src\main\resources\batch.bat to C:\core\target\classes\batch.bat
[DEBUG] file DataObject.hbm.xml has a filtered file extension
[DEBUG] copy C:\core\src\main\resources\com\data\DataObject.hbm.xml …
Run Code Online (Sandbox Code Playgroud) 我有一个A
调用EJB的EJB B
.用户界面不应等待超过30秒的响应.如果缺少某些数据,则应返回部分响应.
如何在EJB上定义超时(30秒的时间限制)B
?
我可以将EJB定义B
为Asynchronous
返回Future
,然后执行Future.get(30, TimeUnit.SECONDS)
.但这是最好的解决方案吗?
谢谢
PS我使用glassfish 3.1
我需要从该 tomcat 上运行的 web 应用程序重新启动 tomcat 服务。所以我尝试执行停止 tomcat 服务的脚本,然后启动它:
echo "before stop" >> textfile.txt
NET STOP "Tomcat7"
:loop
timeout 3
SC query Tomcat7 | FIND "STATE" | FIND "RUNNING" > NUL
IF ERRORLEVEL 1 (
goto start
) ELSE (
goto loop
)
:start
NET START "Tomcat7"
Run Code Online (Sandbox Code Playgroud)
Java代码:
String command = "C:\\Tomcat 7.0\\bin\\restart.bat";
Process p = Runtime.getRuntime().exec(command);
Run Code Online (Sandbox Code Playgroud)
Tomcat 已停止,但未启动。如果我从命令行运行这个批处理,它可以正常工作。
感谢您的时间
我有一个很长的日志文件,用log4j
, 10threads
写入日志。我正在寻找日志分析器工具,它可以找到用户等待很长时间的行(即同一线程的日志条目之间的差异超过一分钟)。
PS 我正在尝试使用OtrosLogViewer
,但它通过某些值(例如,通过线程 ID)进行过滤,并且不会在行之间进行比较。
PPS 新版本的 OtrosLogViewer 有一个“Delta”列,用于计算 adj 日志行之间的差异(以毫秒为单位)
谢谢你
java ×6
java-ee ×2
tomcat ×2
tomcat7 ×2
batch-file ×1
cross-domain ×1
ejb-3.1 ×1
glassfish-3 ×1
indexing ×1
jersey ×1
jpa ×1
jpa-2.0 ×1
jquery ×1
locale ×1
log-analysis ×1
logging ×1
maven ×1
rest ×1
threadpool ×1
timeout ×1