jvm优化

可能出现的异常:
OutOfMemoryError:java heap size
OutOfMemoryError: PermGen space”

两种都是内存溢出,
heap size是说申请不到新的内存了,这个很常见,检查应用或调整堆内存大小。
“PermGen space”是因为永久存储区满了,这个也很常见,一般在热发布的环境中出现,是因为每次发布应用系统都不重启,久而久之永久存储区中的死对象太多导致新对象无法申请内存,一般重新启动一下即可。

tomcat设置自动加载的时候可能会出现

在声明的时候注意点,别在循环里声明新对象,不用了给设null,如果还不够就得考虑增加jvm最大内存数了。。

为什么会产生StackOverflowError?
答:因为一个线程把Stack内存全部耗尽了,一般是递归函数造成的。

参考:
http://blog.csdn.net/chinajane163/article/details/7784521

ActiveMQ helloworld

apache官网下载了5.9.1很扯淡的,3次无法解压成功,换了4次镜像,算是解压好了。。。
activemq.bat运行显示:

WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directo
ry: E:\workspace\apache-activemq-5.9.1\bin\..\data\kahadb only has 35873 mb of u
sable space – resetting to maximum available disk space: 35873 mb
ERROR | Temporary Store limit is 51200 mb, whilst the temporary data directory:
E:\workspace\apache-activemq-5.9.1\bin\..\data\localhost\tmp_storage only has 35
873 mb of usable space – resetting to maximum available 35873 mb.

出了个错误,不过能正常使用,
目前还不知道有什么影响。

后台地址
http://localhost:8161/admin
admin/admin
可以正常打开

运行webappdemo
提示
missing brokerURL (specified via org.apache.activemq.brokerURL init-Param) or embedded broker

呃呃,又找了个简单例子:
代码来源:http://blog.csdn.net/jason5186/article/details/9196041
发送:
public class JmsSender {
public static void main(String[] args) throws JMSException{
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(“tcp://127.0.0.1:61616”);

Connection connection = connectionFactory.createConnection();
connection.start();
;// connection.

Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(“Test.foo”);

MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
for(int i=0; i<100; i++) { int id = i+1; ObjectMessage message = session.createObjectMessage(); message.setObject(new User(id, "张三"+id, "123456")); producer.send(message); } session.commit(); session.close(); connection.close(); } } 之后能在后台看见队列中的内容 接收 public class JmsReceiver { public static void main(String[] args) throws JMSException, InterruptedException{ ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); Connection connection = connectionFactory.createConnection(); connection.start(); final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("Test.foo"); MessageConsumer consumer = session.createConsumer(destination); //listener 方式 consumer.setMessageListener(new MessageListener() { public void onMessage(Message msg) { ObjectMessage message = (ObjectMessage) msg; //TODO something.... try { User user = (User) message.getObject(); System.out.println("收到消息:"+user); } catch (JMSException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { session.commit(); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); TimeUnit.MINUTES.sleep(1); session.close(); connection.close(); } }

servlet中的监听器

http://www.imooc.com/learn/271

慕客好地方,比较全面的说了下监听器的功能和作用。

作用1:
启动的时候初始化,比如像spring那样可以预先加载需要调用的class文件,避免用的时候直接classforname影响性能。
作用2:
统计在线用户,可以监听session的创建和销毁,创建时session监听获得不到ip需要从request监听中获取。

另外Servlet3 中可以通过注解增加监听,不需要修改web.xml。运行环境需要java6,tomcat7。

就酱。