在windows中安装nginx服务

有的服务器难免使用windows操作系统,将nginx安装为windows系统服务后可以随系统自动启动,比较方便。由于nginx本身不提供安装服务的功能,因此需要借助于第三方工具实现,这里介绍其中的一种方式,通过winsw来安装系统服务。

在 https://github.com/kohsuke/winsw 这里可以找到winsw的下载链接,下载后可以放到任意地方,建议就放在和nginx相同的目录。winsw的运行需要DotNet3.5支持,高版本的windows一般不再自带DotNet3.5版本,运行winsw时会弹出对话框自动联网安装,如果网络比较慢,而电脑上刚好有windows的安装盘,可以将windows的安装盘载入后,用离线的方式安装DotNet3.5:

用管理员身份运行cmd窗口,输入以下命令:

Dism.exe /online /enable-feature /featurename:NetFX3 /All /Source:I:\sources\sxs /LimitAccess

其中红色标记的I需要替换成windows安装盘的实际盘符。很快DotNet3.5就离线安装好了。

在winsw.exe所在目录新建一个文件,命名为winsw.xml,内容如下:

<configuration>
  <id>nginx</id>
  <name>nginx</name>
  <description>A Http Server</description>

  <executable>D:\nginx-1.8.0\nginx.exe</executable>
  <arguments></arguments>
</configuration>

其中一些参数可以根据自己情况调整,保存后继续以管理员身份在cmd中输入如下命令:

winsw.exe install

执行成功后在系统服务中就可以看到nginx的服务出现了。

Spring websocket

Spring自从4.0开始提供了对websocket的支持,配合sockjs,可以部分兼容到IE6,websocket终于可以大行其道了。

实际使用中遇到不少问题,逐步列举出来,避免以后忘掉。

  1. 由于浏览器设置了http代理,结果创建websocket时失败,提示:Error in connection establishment: net::ERR_TUNNEL_CONNECTION_FAILED。取消浏览器代理后恢复正常。
  2. 在web.xml中,DispatcherServlet和spring-mvc需要用到的全部filter,都需要加上<async-supported>true</async-supported>,以便在sockjs兼容不支持websocket的浏览器时使用。
  3. 工程使用spring-mvc传统的方式配置,即使用ContextLoaderListener来加载root的Spring-contextConfigLocation(除Controller外的其他Component),使用DispatcherServlet来加载Controller。而处理websocket的业务逻辑写在Controller中,加上了@MessageMapping的注解,结果请求根本到不了Controller里面去。后来发现AbstractMethodMessageHandler负责扫描@MessageMapping,但是在该类的子类实例化时,把Spring的Root Context扔进去了,这里面是没有Controller的类的,所以后面websocket的请求就到不了Controller。将websocket的相关配置文件放到DispatcherServlet里去加载,于是问题解决。

相关配置示例:

web.xml:

<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath*:/applicationContext*.xml</param-value>  
    </context-param>  
  
<listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
  
<servlet>  
        <servlet-name>SpringMVC</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>  
                classpath*:/spring-*.xml  
            </param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
        <async-supported>true</async-supported>  
</servlet>

applicationContext.xml:

<context:component-scan base-package="xxx.xxx">  
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
</context:component-scan>

spring-mvc.xml:

<context:component-scan base-package="x.x.x" use-default-filters="false">  
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
</context:component-scan>

spring-websocket.xml:

<websocket:message-broker application-destination-prefix="/app">  
        <websocket:stomp-endpoint path="/chat">  
            <websocket:sockjs/>  
        </websocket:stomp-endpoint>  
        <websocket:simple-broker prefix="/public,/private"/>  
</websocket:message-broker>

修改Google搜索结果高亮显示效果

最近不知道什么原因,登陆Google账号后,搜索结果显示全部为蓝色加粗,再也看不到红色结果了,退出登录后清空cookie,搜索结果关键词红色高亮恢复。网上也有一些其他人遇到这种情况,但是没有好的解决办法,总不能一直不登陆Google吧。

网上有一种解决方案,使用Tampermonkey来修改页面CSS,Chrome下面可以使用,Firefox应该有类似的解决方案。

新建脚本:

// @match      https://*.google.com/*  
// ==/UserScript==  
var head, style;  
head = document.getElementsByTagName('head')[0];  
if (!head) { return; }  
style = document.createElement('style');  
style.type = 'text/css';  
style.innerHTML = 'em{color: #dd4b39;font-weight:normal;}';  
head.appendChild(style);

搜索关键词红色高亮又回来了!

Sizing the Java heap

Sizing the Java heap

Size your Java heap so that your application runs with a minimum heap usage of 40%, and a maximum heap usage of 70%.

IntroductionAn incorrectly sized Java heap can lead to OutOfMemoryError exceptions or to a reduction in the performance of the Java application.

If the Java heap is smaller than the memory requirements of the application, OutOfMemoryError exceptions are generated because of Java heap exhaustion. If the Java heap is slightly larger than the requirements of the application, garbage collection runs very frequently and affects the performance of the application.

You must correctly size the Java heap based on the real memory usage of the Java application.

 

Sizing the heap based on application memory utilizationSet the maximum Java heap size, using the -Xmx command-line option, to a value that allows the application to run with 70% occupancy of the Java heap.

The Java heap occupancy often varies over time as the load applied to the application varies. For applications where occupancy varies, set the maximum Java heap size so that there is 70% occupancy at the highest point, and set the minimum heap size, using the -Xms command line option, so that the Java heap is 40% occupied at its lowest memory usage. If these values are set, the Java memory management algortihms can modify the heap size over time according to the application load, while maintaining usage in the optimal area of between 40% and 70% occupancy.

Maximum possible heap size and maximum recommended heap size (32-bit Java)The memory space provided by the operating system to the Java process varies by operating system and is used for two separate memory areas: the Java heap and the native heap. Because a finite amount of memory is provided by the operating system, and that memory is shared between the two heaps, the larger the amount of memory that is allocated to the Java heap, using the -Xmx setting, the smaller the native heap becomes. If the native heap is too small, an OutOfMemoryError occurs when it is exhausted, in the same way as for the Java heap.

Platform Additional options Maximum heap size Recommended heap size limit Additional notes
AIX None 3.25 GB 2.5 GB Maximum heap size is not required to be, but should ideally be, a multiple of 256 MB
Linux None 2 GB 1.5 GB
Hugemem Kernel 3 GB 2.5 GB
Windows None 1.8 GB 1.5 GB
/3GB 1.8 GB 1.8 GB

The table shows both the maximum Java heap possible and a recommended limit for the maximum Java heap size setting. The use of a maximum Java heap setting up to the recommended limit is unlikely to reduce the native heap size to a point that the native heap is exhausted.

Before setting a Java heap size greater than the recommended limit, you must understand the level of usage of the native heap, to ensure that the native heap does not become exhausted.

If you are running an application that has particularly high numbers of threads or makes heavy use of Java Native Interface (JNI) code, for example, Type 2 JDBC drivers, you might experience problems relating to the native heap with smaller Java heap sizes than the recommended limit.

Maximum possible heap size and maximum recommended heap size (64-bit Java)When running 64-bit Java, the memory space provided by the operating system to the Java process is very large. You can therefore assume that no limit is imposed on the maximum size of the Java heap because of the contention of memory resource between the Java heap and the native heap.

Java heap size and amount of physical memory availableIt is important to have more physical memory than is required by all of the processes on the machine combined to prevent paging or swapping. Paging reduces the performance of the system and affects the performance of the Java memory management system.

When increasing the Java heap size, ensure that enough unused physical memory is available on the machine to cover the increase. If sufficient physical memory is not available, either install additional memory or take into account the effect on overall performance that occurs.

This requirement does not apply to operating systems running on System z.

Size your Java heap so that your application runs with a minimum heap usage of 40%, and a maximum heap usage of 70%.