lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2009年04月6日

jsp拦截JspWriter实现类似php的ob_get_contents

Filed under: cache,Java — 标签:, , , , — lizongbo @ 22:29

jsp拦截JspWriter实现类似php的ob_get_contents
php可以通过ob_start和ob_get_contents可以拦截并得到echo等方式输出的内容,并可以方便的进行二次处理。java的jsp也是可以非常方便就实现这样的功能,那就是实现一个自定义的JspWriter.
oscache的功能强大的jsp缓存标签就是这样干的。
前段时间作了个简单的rss输出,由于避免访问量过大影响到数据库和接口调用。
于是需要对生成的rss文本进行缓存。原有的jsp代码都已经写好,大概动就太麻烦了,于是用TextJspWriter继承javax.servlet.jsp.JspWriter,然后在jsp作小改造就搞定了。
php的用法参考: http://cn2.php.net/ob_get_contents
TextJspWriter.java的代码为:
[code]

package com.lizongbo.util;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.jsp.JspWriter;

public class TextJspWriter extends JspWriter {
public String getString() {
return sb.toString();
}

private StringBuilder sb = null;

public TextJspWriter() {
this(8192, true);
}

public TextJspWriter(int bufferSize, boolean autoFlush) {
super(bufferSize, autoFlush);
sb=new StringBuilder(bufferSize);
}

@Override
public void clear() throws IOException {
sb = new StringBuilder();

}

@Override
public void clearBuffer() throws IOException {
sb = new StringBuilder();

}

@Override
public void close() throws IOException {

}

@Override
public void flush() throws IOException {

}

@Override
public int getRemaining() {
return 0;
}

@Override
public void newLine() throws IOException {
sb.append(“\n”);

}

@Override
public void print(boolean arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(char arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(int arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(long arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(float arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(double arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(char[] arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(String arg0) throws IOException {
sb.append(arg0);

}

@Override
public void print(Object arg0) throws IOException {
sb.append(arg0);

}

@Override
public void println() throws IOException {
sb.append(“\n”);

}

@Override
public void println(boolean arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(char arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(int arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(long arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(float arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(double arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(char[] arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(String arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void println(Object arg0) throws IOException {
sb.append(arg0);
println();

}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
sb.append(cbuf, off, len);
}

}

[/code]

jsp代码:

[code]
《%//读取缓存
String textStr==rssTextCache.get(cacheKey);
if(textStr==null){
JspWriter oldout=out;
out= new com.lizongbo.util.TextJspWriter();
%》
《% //正常输出rss内容%》

《%
textStr=((com.lizongbo.util.TextJspWriter)out).getString();
out=oldout;//放入缓存中
rssTextCache.put(cacheKey,textStr);
}
%》
《%=textStr%》

[/code]

2007年12月12日

体验OpenFire3.4.2的集群功能

Filed under: cache,Java — 标签:, , , , , — lizongbo @ 15:28

体验OpenFire3.4.2的集群功能

曾经因为学习xmpp的时候关注了openfire,那时候还叫WildFire,
曾经照虎画猫用lumaqq写了个qq插件,后来没时间也就没看了.

OpenFire 3.4.2发布了,在3.4开始就支持了集群功能,

changelog在: http://www.igniterealtime.org/builds/openfire/docs/latest/changelog.html

相关下载链接为:

http://www.igniterealtime.org/downloadServlet?filename=openfire/openfire_3_4_2.zip
http://www.igniterealtime.org/downloadServlet?filename=openfire/openfire_src_3_4_2.zip

http://www.igniterealtime.org/downloadServlet?filename=smack/smack_src_3_0_4.zip

http://www.igniterealtime.org/downloadServlet?filename=spark/online/spark_2_5_8_online.exe

http://www.igniterealtime.org/downloadServlet?filename=connectionmanager/connection_manager_src_3_4_2.zip
http://www.igniterealtime.org/downloadServlet?filename=connectionmanager/connection_manager_3_4_2.zip

上面的下载连接不支持断点续传.

http://www.igniterealtime.org/projects/openfire/plugins/asterisk-im.jar
http://www.igniterealtime.org/projects/openfire/plugins/broadcast.jar
http://www.igniterealtime.org/projects/openfire/plugins/contentFilter.jar
http://www.igniterealtime.org/projects/openfire/plugins/sip.jar
http://www.igniterealtime.org/projects/openfire/plugins/gateway.jar
http://www.igniterealtime.org/projects/openfire/plugins/presence.jar
http://www.igniterealtime.org/projects/openfire/plugins/registration.jar
http://www.igniterealtime.org/projects/openfire/plugins/search.jar
http://www.igniterealtime.org/projects/openfire/plugins/subscription.jar
http://www.igniterealtime.org/projects/openfire/plugins/userImportExport.jar
http://www.igniterealtime.org/projects/openfire/plugins/userservice.jar

http://www.igniterealtime.org/projects/openfire/plugins-beta/filetransferdisabler.jar
http://www.igniterealtime.org/projects/openfire/plugins-beta/red5.war
http://www.igniterealtime.org/projects/openfire/plugins-beta/vcard.jar

http://www.igniterealtime.org/projects/openfire/plugins/enterprise.jar
http://www.igniterealtime.org/projects/openfire/plugins/webchat.war

配置openfire的启动参数,比如设置内存,
如果是用openfired.exe启动,则在D:\Java\openfire\bin创建openfired.vmoptions
内容为:
-Xms64m
-Xmx256m

如果是用openfire-service.exe进去启动的,则创建openfire-service.vmoptions.

测试了一下集群功能,openfire clustering特性用的是被Oracle收购的coherence来实现的,授权用的Tangosol Coherence: Application Edition.
tangosol-license.jar中有licensee信息,
coherence的下载地址在:
http://download.oracle.com/otn/java/coherence/coherence-331.zip
来源: http://www.oracle.com/technology/software/products/ias/htdocs/coherence.html

2007年11月24日

Jgroups配置与使用的学习笔记

Filed under: Java,JbossCache — 标签:, — lizongbo @ 14:56

Jgroups配置与使用的学习笔记
1.相关资料:
关于jgroups有几篇很好的中文介绍文章:
Rene Xu 的《Multicast and JGroups(未完待续)》
http://renexu.blogspot.com/2006/08/multicast-and-jgroups_28.html
还有Tim 的《JGroups 简介、适用场合、配置、程序例子Demo等完全使用指南》
http://hi.baidu.com/jabber/blog/item/7e879852a23efd0f0cf3e3ea.html
和Puras 的《JGroups之皮毛》
http://www.javaeye.com/post/292587

IP组播技术概述的资料:
http://net.pku.edu.cn/webCourse/ppt/lec14.pdf

jgroups的向导:
http://www.jgroups.org/javagroupsnew/docs/tutorial/html_single/index.html
jgroups的用户手册:
http://www.jgroups.org/javagroupsnew/docs/manual/html_single/index.html

2.jgroups 2.5及以上版本要求的jdk最低版本至少为JDK5.0,最小依赖的库文件为:commons-logging.jar,
jgroups-all.jar是一个包含了demo和junit单元测试用例的jar,如果想得到精简了的jar,
则可以下载源代码下来,使用 jar 任务编译得到jgroups-core.jar

操作步骤如下,解压JGroups-2.5.0.src.zip,得到D:\Java\JGroups-2.5.0.src。
然后命令行下运行build jar。

[code]

D:\Java\JGroups-2.5.0.src\lib>cd ..

D:\Java\JGroups-2.5.0.src>build jar
Buildfile: build.xml

prepare:
[mkdir] Created dir: D:\Java\JGroups-2.5.0.src\classes
[mkdir] Created dir: D:\Java\JGroups-2.5.0.src\dist
[mkdir] Created dir: D:\Java\JGroups-2.5.0.src\dist\javadoc

compile:
[javac] Compiling 508 source files to D:\Java\JGroups-2.5.0.src\classes
[javac] 注意:某些输入文件使用或覆盖了已过时的 API。
[javac] 注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。
[javac] 注意:某些输入文件使用了未经检查或不安全的操作。
[javac] 注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。

jgroups-all.jar:
[jar] Building jar: D:\Java\JGroups-2.5.0.src\dist\jgroups-all.jar

jgroups-core.jar:
[jar] Building jar: D:\Java\JGroups-2.5.0.src\dist\jgroups-core.jar

jar:

BUILD SUCCESSFUL
Total time: 2 minutes 37 seconds

[/code]
jgroups-core.jar比jgroups-all.jar小接近一半,在生产环境中,使用jgroups-core.jar即可。

3.查看jgroups版本信息: java -jar jgroups-all.jar

[code]

D:\Java\JGroups-2.5.0.src>cd dist

D:\Java\JGroups-2.5.0.src\dist>java -jar jgroups-all.jar

Version:        2.5.0
CVS:            $Id: Version.java,v 1.54 2007/07/02 14:39:09 belaban Exp $
History:        (see doc/history.txt for details)

[/code]

4.启动draw的demo。
运行 D:\Java\JGroups-2.5.0.src\bin\draw.bat,却提示出错。
[code]
2007-11-12 10:18:54 org.jgroups.stack.Protocol setPropertiesInternal
警告: down_thread was deprecated and is ignored
2007-11-12 10:18:54 org.jgroups.stack.Protocol setPropertiesInternal
警告: up_thread was deprecated and is ignored
2007-11-12 10:18:54 org.jgroups.stack.Protocol setPropertiesInternal
警告: down_thread was deprecated and is ignored
2007-11-12 10:18:54 org.jgroups.stack.Protocol setPropertiesInternal
警告: up_thread was deprecated and is ignored

——————————————————-
GMS: address is 10.108.20.126:2624
——————————————————-
2007-11-12 10:18:55 org.jgroups.stack.RouterStub connect
警告: RouterStub[local_address=10.108.20.126:2624,router_host=localhost,router_p
ort=5556,connected=false] failed connecting to localhost:5556
2007-11-12 10:18:55 org.jgroups.protocols.TUNNEL handleDownEvent
严重: failed connecting to GossipRouter at localhost:5556
2007-11-12 10:18:56 org.jgroups.stack.GossipClient _register
严重: exception connecting to host 127.0.0.1:5556
2007-11-12 10:18:58 org.jgroups.stack.GossipClient _getMembers
严重: exception connecting to host 127.0.0.1:5556
2007-11-12 10:18:58 org.jgroups.protocols.TCPGOSSIP sendGetMembersRequest
严重: [FIND_INITIAL_MBRS]: gossip client found no members
** View=[10.108.20.126:2624|0] [10.108.20.126:2624]
2007-11-12 10:19:00 org.jgroups.stack.GossipClient _getMembers
严重: exception connecting to host 127.0.0.1:5556
2007-11-12 10:19:00 org.jgroups.protocols.TCPGOSSIP sendGetMembersRequest
严重: [FIND_INITIAL_MBRS]: gossip client found no members
2007-11-12 10:19:05 org.jgroups.stack.GossipClient _getMembers
严重: exception connecting to host 127.0.0.1:5556
2007-11-12 10:19:05 org.jgroups.protocols.TCPGOSSIP sendGetMembersRequest
严重: [FIND_INITIAL_MBRS]: gossip client found no members
2007-11-12 10:19:07 org.jgroups.stack.GossipClient _getMembers

[/code]

4.启动drawnio的demo。

需要编辑D:\Java\JGroups-2.5.0.src\bin\drawnio.bat

将第10行LOCALHOSTIP的值改为本机真实 ip,LOCALHOSTIP=192.168.18.1

将最后的 “-props c:\jboss\JGroups\conf\tcp-nio.xml”改为“-props ..\conf\tcp-nio.xml”

修改后的D:\Java\JGroups-2.5.0.src\bin\drawnio.bat为:

[code]

@rem Convenience launcher for the Draw demo (contributed by Laran Evans lc278@cornell.edu)
@echo off

set CPATH=../classes;../conf;../lib/commons-logging.jar;../lib/log4j.jar;../lib/log4j-1.2.6.jar;../lib/concurrent.jar;../conf/log4j.properties

set JAVA_OPTS=
if -debug==%1 set JAVA_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_shmem,server=y,suspend=y,address=jgc1

if “%LOCALHOSTIP%”==”” echo Warning: You should set environment variable ‘LOCALHOSTIP’ to your local ip address before running this script.
if “%LOCALHOSTIP%”==”” set LOCALHOSTIP=10.108.20.126

@echo on
java -classpath %CPATH% %JAVA_OPTS% -Djgroups.bind_addr=%LOCALHOSTIP% -Djgroups.tcpping.initial_hosts=%LOCALHOSTIP%[7800],%LOCALHOSTIP%[7801] org.jgroups.demos.Draw -props ..\conf\tcp-nio.xml
[/code]

运行drawnio.bat,重复执行两次(或者三次),可以看到两个窗口(或者三个),在任意一个窗口上画图或者进行“clear”操作,图像信息均会被同步到其它窗口 。

5.测试两组不同的draw demo。

1.保持drawnio.bat与tcp-nio.xml的内容不变,将drawnio.bat运行两次。

2.修改drawnio.bat,-Djgroups.tcpping.initial_hosts=%LOCALHOSTIP%[7800],%LOCALHOSTIP%[7800]修改为 -Djgroups.tcpping.initial_hosts=%LOCALHOSTIP%[7803],%LOCALHOSTIP%[7804]。
修改tcp-nio.xml,start_port=”7803″改为 start_port=”7803″
(也可在bat里加上参数 -groupname aa)
将drawnio.bat运行两次。

在这启动的四个窗口中,两两分别为一组,信息同步。

6.分别在不同的机器上测试 draw demo.
修改drawnio.bat,-Djgroups.tcpping.initial_hosts=%LOCALHOSTIP%[7800],%LOCALHOSTIP%[7800]修改为 -Djgroups.tcpping.initial_hosts=10.108.20.126[7803],10.108.20.33[7803].

Older Posts »

Powered by WordPress