http://blogs.mulesoft.org/load-balancing-tomcat-7-using-iis-7-5/


Front-ending Apache Tomcat with Apache Web Server or  is sometimes thought to improve performance. However, performance of  standalone has already been known to be very good. So why add IIS or Apache web server in front of it? – the answer is scalability and maintenance. Front-ending Tomcat with such web servers allows you to add more instances in case of increased load and also bring down instances for maintenance/upgrades.

This blog shows you end-to-end, step-by-step detailed instructions on how to setup such an environment. It should take you about an hour to configure this setup for yourself. Follow the instructions slowly and carefully to make sure you do not skip/miss any step. Here is a higher level view of the setup.

Environment: Windows 7 Professional, , IIS 7.5, Java SE 6 Update 29

You need to perform the following steps:-

  1. Install Java
  2. Install IIS
  3. Install Tomcat
  4. Configure Tomcat
  5. Configure IIS
  6. Create and deploy a web application
  7. Test

Install Java

You will be installing Java SE 6 Update 29. Download the JDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html. Once downloaded, double-click on the exe file to kick-start the installation. In the installation wizard, accept the defaults and finish the installation.

You now need to set the JAVA_HOME environment variable to point to the JDK installation directory.

  1. Open Windows Explorer
  2. Right-click on Computer and select Properties. This opens the System Properties window. Click on the Advanced tab and then click on Environment Variables
  3. In the section User variables for Administrator, click on the New button. Enter JAVA_HOME forVariable name:. For Variable value, enter the absolute path of the directory where the JDK is installed. For example, on my machine it is installed at C:\Program Files\Java\jdk1.6.0_29

Install IIS
Installing IIS is fairly straightforward. Apart from IIS, you will also install some extra components, which you will see shortly.

  1. Go to the Control Panel and click on Programs and Features. Click on Turn Windows features on or off.
  2. In the Windows Features window, check Internet Information Services and Microsoft .NET Framework 3.5.1.
  3. Expand Internet Information Services and under World Wide Web Services – Application Development Features, check ISAPI Extensions  and  ISAPI Filters.
  4. Click OK to finish installation.

Install Tomcat 7

  1. Download Tomcat 7 from http://tomcat.apache.org/download-70.cgi. Download the 64-bit Windows Zip file.
  2. Unzip the file to your c:\ drive and a directory named apache-tomcat-7.0.xx will be created, where xx is the minor version number.
  3.  Rename that directory to tomcat.
  4. This finishes your Tomcat installation.

In order to distribute load across multiple instances of Tomcat, we need to create two more instances of Tomcat.

  1. Create two directories named tomcat1 and tomcat2, directly under c: drive.
  2. Within both these directories, create the following sub-directories – bin, conf, logs, webapps, temp, work.
  3. From c:\tomcat\conf, copy server.xml, web.xml, context.xml and tomcat-users.xml, and paste them into the conf directories of tomcat1 and tomcat2
  4. Copy startup.bat and shutdown.bat from c:\tomcat\bin and paste them in the bin directories of c:\tomcat1\bin and c:\tomcat2\bin
  5. Edit startup.bat and shutdown.bat of tomcat1 and at the very top of these files, add the following lines:

set CATALINA_HOME=c:\tomcat
set CATALINA_BASE=c:\tomcat1

Repeat step 5 for tomcat2 with the exception that CATALINA_BASE should be set to c:\tomcat2

Configure Tomcat
Lets go through some concepts/terminology before we begin configuration.

connector in Tomcat is something which allows a client to connect to Tomcat. Tomcat ships will multiple connectors e.g. HTTP, AJP etc. By default, the HTTP connector is configured to listen on port 8080. In our case, the IIS server will act as a client to Tomcat. IIS web server will communicate with Tomcat using the AJP protocol. We need to ensure that we have the ports for the AJP connector configured. We also want all requests to be routed through IIS and do not want anybody to bypass it, i.e we do not want anybody to access the web application through port 8080 using the HTTP connector. Hence, we will disable (comment) the HTTP connector from our configuration.

worker is simply an instance of Tomcat to which IIS will dispatch requests. In this case, all three Tomcat instances will be workers. Each worker needs to have a unique id, which will be assigned to it using the jvmRoute attribute of the <Engine> element in server.xml. You will see later how and where this id is used by IIS server. Lets start configuring the Tomcat instances:

  • Change Shutdown ports:- All three Tomcat instances will reside on the same machine, hence we need to ensure that ports do not conflict. Shutdown ports are where Tomcat listens for the shutdown command. Open server.xml of each tomcat1, locate the <Server > element start tag and change the port number to 8006. Repeat this step for tomcat2 and change the port number to 8007.
  • Change AJP ports :- In server.xml of tomcat1, locate the <Connector> element whoseprotocol=”AJP/1.3″ and change the port number to 8010. Similarly for tomcat2, change the port number to 8011
  • Disable HTTP Connectors:- In the server.xml of each Tomcat instance, locate the<Connector> element shown below and either delete it or comment it out

    <Connector port=”8080″ protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”8443″ />

  • Assign worker ids:- In server.xml of tomcat, locate the <Engine> element and add the following attribute to its start tag jvmRoute=”worker0″ (the value of the attribute is workerZERO and notworkerOH). Repeat this step for tomcat1 and assign the value of worker1. Similarly repeat for tomcat2 and assign the value of worker2.

Configure IIS
You will now configure ISAPI redirector plug-in provided by Tomcat. The redirector requires 4 entities:-

  • isapi_redirect.dll : This is the IIS plugin which you will download later
  • worker.properties: A file that describes the hosts and ports of workers (Tomcat instances/processes)
  • uriworkermap.properties: A file that maps URL patterns to workers. Allows you to specify as to which worker will handle the matching URL path request
  • isapi_redirect.properties: A file which tells IIS the location of all of the above files.

Lets start the configuration. After you installed IIS, it would have created a directory named c:\inetpub. All configuration will be done in the c:\inetpub directory.

  1. Under c:\inetpub, create a subdirectory called isapi. Under c:\inetpub\isapi, create three subdirectories – bin, logs, conf.
  2. To download the isapi_redirect.dll, go to  http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/ and download the zip appropriate for your system i.e. 32-bit or 64-bit.
  3. Unzip the downloaded file and copy isapi_redirect.dll and paste it under c:\inetpub\isapi\bin
  4. Inside c:\inetpub\isapi\conf, create a file named worker.properties. In this file you will create workers, their host and the port on which they will communicate with IIS. Workers are of different types e.g. ajp13 is a worker which communicates with web server  i.e. tomcat instances , lb is the worker which will act as the load balancer, stats worker type will give you load balancing statistics and status information. You will be adding key-value pairs, each key must begin with the wordworker. The complete worker.properties file is shown below. Copy/paste the below to your worker.properties file and save it. Explanation of each line is given above the line in a comment
    12345678910111213141516171819202122232425262728293031323334353637
    #The list of workers
    worker.list=balancer,stats
     
    #The template contains default values. Each worker will inherit
    #these values and override the port
     
    # the worker type
    worker.template.type=ajp13
    #This could also be an IP address. Since all Tomcat instances
    #and IIS are installed on the same host, hence "localhost"
    #will suffice as the value
    worker.template.host=localhost
    # AJP port should match the one in server.xml
    worker.template.port=8009
    #load balancing factor
    worker.template.lbfactor=1
     
    #worker0 inherits everything from template
    worker.worker0.reference=worker.template
     
    #worker1 inherits from template but overrides port
    worker.worker1.reference=worker.template
    worker.worker1.port=8010
     
    #worker2 inherits from template but overrides port
    worker.worker2.reference=worker.template
    worker.worker2.port=8011
     
    #The load balancer worker
    worker.balancer.type=lb
    worker.balancer.sticky_session=1 #session is sticky
    #List of workers amongst which load is distributed
    worker.balancer.balance_workers=worker0,worker1,worker2
     
    #The worker stats will be of type status
    #will get load balancing statistics
    worker.stats.type=status
    view rawworker.properties hosted with ❤ by GitHub
  5. Inside c:\inetpub\isapi\conf, create a file named uriworkermap.properties. In this file you will map the workers to the URL path patterns. This will tell IIS, which worker will handle a request for a given URL path. As you can see below, the /foo web application is mapped to the load balancer worker, which the /jkstatus path is mapped to the stats worker.
    12345
    #/foo and /foo/* are both mapped to balancer worker
    /foo|/*=balancer
     
    #/jkstatus is mapped to the stats worker
    /jkstatus/=stats
  6. Inside c:\inetpub\isapi\bin, create a file named isapi_redirect.properties. This file will tell IIS, the location of isapi_redirect.dll, worker.properties, uriworkermap.properties and the location of the log file for the redirector plugin. You can copy/paste contents from the file as shown below.
    123456789
    #tomcat will be the name of the virtual directory under IIS
    extension_uri=/tomcat/isapi_redirect.dll
    #location of the log file
    log_file=C:\inetpub\isapi\logs\isapi_redirect.log
    log_level=debug
    #location of worker.properties
    worker_file=C:\inetpub\isapi\conf\worker.properties
    #location of uriworkermap.properties
    worker_mount_file=C:\inetpub\isapi\conf\uriworkermap.properties

The remainder of the configuration is through the IIS Manager:

  1. To open the IIS Manager, open  Administrative Tools from the Control Panel and then click onInternet Information Services (IIS) Manager
  2. You will now create a Virtual Directory named tomcat. Under Connections, navigate to [[Root Node]] > Sites > Default Web Site. Right-click on Default Web Site  and select Add Virtual Directory. ForAlias enter tomcatand for Physical Path, enter C:\inetpub\isapi\bin. Click OK to close the Edit Virtual Directory window.
  3. To configure Handler Mappings, under Default Web Site, select tomcat. In the Features View pane, double-click on Handler Mappings and then select ISAPI-dllwhich should be disabled at the moment. Now in the Actions pane, select Edit feature permissions. In the Edit Feature Permissions window, check the Execute option and click OK to close the window. ISAPI-dll should now be enabled.
  4. You now need to configure the ISAPI Filter and specify the location of the dll. Select Default Web Site . In the Features View pane, double-click on ISAPI Filters. From the Actions pane, click onAdd. For Filter Name, enter isapi_redirect. For Executable, enter the absolute path of the location of the isapi_redirect.dll – C:\inetpub\isapi\bin\isapi_redirect.dll.
  5. Finally, you have to now enable the ISAPI extension which you have configured above. In theConnections pane, click on the root node. In the Features View pane, double-click on ISAPI and CGI Restrictions. In the Actions pane, click on Add. This will open the Edit ISAPI or CGI Restrictions window. For the ISAPI or CGI Path field, enter the absolute path of isapi_redirect.dll – C:\inetpub\isapi\bin\isapi_redirect.dll. In the Description field, enter isapi_redirect. Also make sure to check the option Allow extension path to execute. Click OK to close this window.
  6. This step  is only for those who have downloaded a 32 bit version of isapi_redirect.dll, but have a 64 bit operating system. If you already downloaded the 64 bit version, then please ignore this step. Click on [[Root Node]] > Application Pools. In the Features View pane, select DefaultAppPool and then click on Advanced Settings… in the Actions pane. In the Advanced Settings window, under the(General) section, set Enable 32-Bit Applications to true. Click OK to close the Advanced Settings window.

Create and deploy a web application
You will now create a web application named foo and deploy it to all the Tomcat instances. You can perform the following steps with Notepad, no need to fire up an IDE. You will be able to copy/paste most of the stuff.

  1. In c:\tomcat\webapps, create a directory named foo. Within this directory, create a subdirectory named WEB-INF. Within WEB-INF, create two sub-directories named classes and lib.
  2. Within the foo directory, create a file named index.jsp. This JSP will simply display the Session ID and Session Creation Time . You can copy the code snippet (below) into the JSP.
    12345678
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD><TITLE>Apache Tomcat Examples</TITLE>
    <META http-equiv=Content-Type content="text/html">
    </HEAD>
    <BODY>
    <h3>Session ID = <%=session.getId()%></h3>
    <h3>Creation Time = <%=session.getCreationTime()%></h3>
    </BODY></HTML>
    view rawindex.jsp hosted with ❤ by GitHub
  3. Copy the foo directory (and all of its contents) into c:\tomcat1\webapps and c:\tomcat2\webapps

Test
Its now time to test your setup.

  1. Start each tomcat instance, but double-clicking on startup.bat in the bin directories of each tomcat.
  2. To start IIS,  go to IIS Manager and Start or Restart (whichever is applicable) IIS from the Actionspane.
  3. Open a browser and navigate to http://localhost/foo. You should see the output from index.jsp. Notice how the value of jvmRoute attribute has been suffixed to the session id. This tells you the tomcat instance serving this request
  4. Open another browser and navigate to http://localhost/foo, you should now see a different tomcat instance servicing the request. Try it with a third browser and you should see the response from the third instance.

Congratulations, you have configured load balancing using Tomcat 7 and IIS 7.5 !

You’ve had to edit configuration files for different Tomcat instances. This requires you to switch between those instances frequently. MuleSoft Tcat – Enterprise Tomcat , has the capability to provide you with a unified front-end for all your Tomcat instances. You would have been able to perform operations (startup and shutdown) and edit configurations of multiple Tomcat instances, all from the Administration Console.

No related posts.

Posted by [czar]
,

function dateToYMD(ymd) {

var date = new Date(ymd);

        var d = date.getDate();

        var m = date.getMonth() + 1;

        var y = date.getFullYear();

        //return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);

return (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);

}


var chart1_x = [];

var chart1_data1 = [];

var chart1_data2 = [];



#foreach($!{data} in $!{dataList})

chart1_x.push([dateToYMD('$!{data.dt}')]);

chart1_data1.push([10$!{data.payAmt}]);

chart1_data2.push([1$!{data.cancAmt}]);

#end


function chart1() {

$('#chart1').highcharts({

            chart: {type: 'column', backgroundColor:'Transparent', size:'100%'},

credits: {enabled: false},

            title: {text: ''},

            subtitle: {text: ''},

            xAxis: {categories: chart1_x},

            yAxis: {

                min: 0,

                title: {

                    text: '금액(원)'

                }

            },

            tooltip: {

                headerFormat: '<span style="font-size:12px">{point.key}</span><table>',

                pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +

                    '<td style="padding:0"><b>{point.y:.0f} 원</b></td></tr>',

                footerFormat: '</table>',

                shared: true,

                useHTML: true

            },

            plotOptions: {

                column: {

                    pointPadding: 0.2,

                    borderWidth: 0

                }

            },

            series: [{

                name: '결제',

                data: chart1_data1

            }, {

                name: '취소',

                data: chart1_data2


            }]

        });

}


Posted by [czar]
,

//<![CDATA[

$(function () {

    var chart_categories = [];

    var chart_data1 = [];

    var chart_data2 = [];


    #foreach($!{data} in $!{dailySum})

    chart_categories.push([dateToYMD('$!{data.DT}')]);

    chart_data1.push([$!{data.TOTAL}]);

    chart_data2.push([$!{data.TOTAL_SUM}]);

    #end


    var chart;

    $(document).ready(function () {

        chart = new Highcharts.Chart({

            chart: {

                renderTo: 'chart_chart1',

                zoomType: 'y',

                backgroundColor: 'Transparent',

                size: '100%'

            },

            credits: {

                enabled: false

            },

            title: {

                text: '그래프1',

                margin: 50

            },

            xAxis: {

                categories: chart_categories,

                type: "datetime",

                tickInterval: 1

            },

            yAxis: [{ // Impression yAxis

                allowDecimals: false,

                min: 0,

                minRange: 5,

                maxPadding: 0.1,

                title: {

                    text: '금액(원)',

                    style: {

                        color: '#6D9F7D'

                    }

                },

                labels: {

                    style: {

                        color: '#6D9F7D'

                    }

                },

                opposite: false

            }, { // % yAxis

                alignTicks: false,

                gridLineWidth: 0,

                min: 0,

                max: 100,

                title: {

                    text: '%',

                    style: {

                        color: '#4399AA'

                    }

                },

                labels: {

                    formatter: function () {

                        return this.value + '%';

                    },

                    style: {

                        color: '#4399AA'

                    }

                },

                opposite: true

            }],

            legend: {

                margin: 20

            },

            tooltip: {

                formatter: function () {

                    var unit = {

                        'Monthly': '원',

                        'Daily': ' 원',

                        '%': '%'

                    }[this.series.name];


                    var value = "";

                    switch (unit) {

                    case '%':

                        value = Highcharts.numberFormat(this.y, 2, ".", ",");

                        break;

                    default:

                        value = Highcharts.numberFormat(this.y, 0, "", ",");

                        break;

                    }

                    return '' + this.x + ': ' + value + ' ' + unit;

                }

            },

            series: [{

                type: 'column',

                name: 'Monthly',

                color: '#A0C35A',

                data: chart_data2,

                visible: false

            }, {

                type: 'spline',

                name: 'Daily',

                color: '#AC80BA',

                data: chart_data1

            }, {

                type: 'spline',

                yAxis: 1,

                name: '%',

                color: '#157984',

                data: [10, 20, 30],

                visible: false

            }]

        });

    });

});

//]]>


Posted by [czar]
,

출처 : http://blog.naver.com/miniblog/100032908730



기간별 매출현황 통계를 내야합니다..ㅠ

일단위, 주단위, 월단위, 연단위로 해야하네요..ㅠㅠ

 

쿼리에서 단위를 group by 식으로 묶어서 msSQL 자체내에서 가능한가요??

일단 order_date라는 컬럼이 있어서 그 컬럼을 기준으로 데이타를 뽑아내야하는데 도저히 모르겠네요..ㅠ

order_date라는 주문일 컬럼을 기준으로 매출건수랑 매출액만 뽑으면 되는데.. 저한테는 너무 어렵네요

 

고수님들의 많은 가르침 부탁드립니다

 

이건 참고할만한 php 소스를 우연히 구했네요

$sql = "SELECT DATE_SUB(order_date, INTERVAL DAYOFWEEK(order_date)-1 DAY) AS date, DATE_ADD(order_date, INTERVAL 7-DAYOFWEEK(order_date) DAY) AS aftDate, COUNT(payway) AS orderNum, SUM(t_amount) AS salesNum FROM ".$db_tbname[order_admin]." $innerQuery GROUP BY date ORDER BY $colSort $limit";

 

주단위로 끊는 소스인데 우리랑 상황이 비슷해서 올려봅니다

mssql로 변형시켜주시면 감사드리겠습니다..ㅠㅠ

 

 

주문테이블에 order_dt(주문 일자 날짜형), order_no(주문번호)로 구성이

되었다고 할때

간단히 다음과 같이 하시면 됩니다.

 

-- 일단위
Select DATEPART(dd, order_dt), count(order_no) From 주문테이블
group by DATEPART(dd, order_dt)
order by DATEPART(dd, order_dt)

 

-- 주 단위
Select DATEPART(ww, order_dt), count(order_no) From 주문테이블
group by DATEPART(ww, order_dt)
order by DATEPART(ww, order_dt)

 

-- 월단위
Select DATEPART(mm, order_dt), count(order_no) From 주문테이블
group by DATEPART(mm, order_dt)
order by DATEPART(mm, order_dt)

 

-- 년단위
Select DATEPART(yy, order_dt), count(order_no) From 주문테이블
group by DATEPART(yy, order_dt)
order by DATEPART(yy, order_dt)

 

 

--분기별

 

SELECT A.memyear,A.AA,SUM(A.CNT) AS CNT
FROM ( 
Select DATEPART(yy, regdate) as memyear,DATEPART(mm, regdate) as memmonth,  count(regdate) as cnt
,(CASE DATEPART(mm, regdate) WHEN '1' THEN '1' WHEN '2' THEN '1' WHEN '3' THEN '1' 
WHEN '4' THEN '2' WHEN '5' THEN '2' WHEN '6' THEN '2' 
WHEN '7' THEN '3' WHEN '8' THEN '3' WHEN '9' THEN '3' 
WHEN '10' THEN '4'
WHEN '11' THEN '4'
WHEN '12' THEN '4' ELSE '0' END) AS AA
From SMEM01MEM
group by DATEPART(mm, regdate),DATEPART(yy, regdate)
--order by DATEPART(ww, regdate) 
) A
group by A.memyear,A.AA
order by A.memyear,A.AA


Posted by [czar]
,



Posted by [czar]
,


http://truepia.tistory.com/230


1. jdk6 다운로드 및 설치
  http://www.oracle.com/technetwork/java/javase/downloads/index.html

2. tomcat6 다운로드 및 설치
  http://tomcat.apache.org/download-60.cgi

3. http://localhost:8080/ 접속으로 tomcat6 정상설치 확인

4. tomcat6 설치폴더 하위의 conf\server.xml에 host 추가 후 tomcat restart
  <Host name="api.test.com" appBase="D:\TestWeb\">
    <Context path="" docBase="D:\TestWeb\" debug="0">
      <Logger className="org.apache.catalina.logger.FileLogger" prefix="api.test.com_log." suffix=".txt" timestamp="true" />
    </Context>
  </Host>

5. D:\TestWeb\ 폴더에 테스트 jsp 생성 후 정상동작 확인
  <% out.println("test"); %>

6. iis에서 사이트 하나 만듬
  - 폴더는 4번에 지정한 동일한 폴더로 지정
  - 실행권한은 "스크립트 및 실행 파일" 로 지정

7. tomcat connectors 다운로드 및 설치
  http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.15/

8. 7번의 설치폴더 하위의 conf\uriworkermap.properties 파일에 다음 내용 추가
  /*.jsp=wlb
  /*.do=wlb

9. iis의 ISAPI 필터 추가 
  - 필터이름 : jakarta
  - 실행파일 : 7번의 설치폴더의 bin\isapi_redirect.dll 설정

10. iis에서 가상디렉터리 설정
  - 경로명은 "jakarta"로 설정하고 경로는 9번의 폴더(isapi_redirect.dll 이 설치된 폴더)를 설정

11. 웹서비스 확장에서 isapi_redirect.dll 실행 가능하도록 추가
  - 새 웹 서비스 확장 추가 클릭
  - 확장 이름 : jakarta
  - 필요한 파일 -> 추가(bin\isapi_redirect.dll 설정)

12. iis, tomcat 재시작

Posted by [czar]
,

Trello로 효율적으로 협업하기


출처 : 
http://prodevelop.wordpress.com/2012/08/31/trello-google-docs%EC%99%80-%ED%95%A8%EA%BB%98-%ED%9A%A8%EC%9C%A8%EC%A0%81%EC%9C%BC%EB%A1%9C-%ED%98%91%EC%97%85%ED%95%98%EA%B8%B0/


원문 : http://www.uservoice.com/blog/founders/trello-google-docs-product-management/

Trello는 매우 개방적인 툴이다. Trello는 한가지 방법으로만 사용하도록 제한하지 않았기 때문에, Trello를 제대로 이용하려면 규칙을 잘 만들어야 한다.

다행히 몇번의 시행착오를 거친후 우리에게 맞는 방법을 발견했고, 그 과정에서 겪은 경험과 노하우를 공유하려고 한다.

우리가 만든 협업 프로세스

우리는 개발 프로세스를 6개의 Trello 보드로 구성했다. 이 보드들의 중심에는 Current Development 보드가 있다. 나머지 모든 보드의 목적은 Current Development 보드의 Next Up 리스트로 카드(기능 향상이나 버그 픽스에 관한)를 보내는데 있다. Next Up 리스트은 모든 엔지니어와 디자이너가 다음 할일을 찾기 위해 뒤져보는 가장 중요한 곳이다.

각 보드의 역할에 대해 깊이 들어가기 전에, 우리 개발 프로세스의 핵심인 Trello Cards 에 대해 살펴보자.


Posted by [czar]
,

String.format vs. MessageFormat.format vs. String.+ vs. StringBuilder.append

Usually I'm not a performance hacker, i.e. I seldom use the final keyword, and I dare to use List.size() in loop expressions. But I'm a little bit sensitive when it comes to string operations, as they can cause severe performance problems, and sometimes security issues as well, e.g., if an SQL query is to be created (ok, bad example, one can use PerparedStatement instead of hacking an SQL string). For logging I often have to concatenate strings, such as
  1. if (log.isLoggable(Level.INFO)) {  
  2.     log.info("Command created - cmd=" + cmd); //$NON-NLS-1$  
  3. }  
Actually I'm not writing lines like this myself, I let log4e do the job -- I really love that tool! But sometimes these string concatenations are hard to read, using a String.format orMessageFormat.format would make it much easier. Here are some examples:
  1. foo(String.format("Iteration %d, result '%s'", i, value[i%2]));  
  2.   
  3. foo(MessageFormat.format("Iteration {0}, result ''{1}''", i, value[i%2]));  
  4.   
  5. // single line:  
  6. foo( "Iteration " + i + ", result '" + value[i%2] + "'");  
  7.   
  8. // multi line:  
  9. String s;  
  10. s = "Iteration ";  
  11. foo(s); s+= i; foo(s); s+= ",\nresult '";  
  12. foo(s); s+= largeString(i); foo(s); s+= "', '";   
  13. foo(s); s+= largeString(i+1); foo(s); s+= "'";  
  14.   
  15. foo(new StringBuilder("Iteration '")  
  16.     .append(i).append(", result '").append(value[i%2]).append("'")  
  17.     .toString());  
(Update: the multi line test was added after daObi and Laurent commented the first version) Hmm... maybe the "+" isn't that bad ;-) I do not understand why String.formatandMessageFormat.format have these different notations, and frankly I seldom get the format string right in the first run. So, besides the aesthetics, what about performance. What would you guess? My guess was StringBuilder would be the fastest, followed by the format methods, and the String operator "+" would be the slowest as new Strings are to be created all along. I run a little test (calling these methods 100000 times, foo() is doing nothing and was, besides other things, only added in order to avoid too much compiler optimizations, although I have no idea if that worked ;-) ). Note that in the test only a few strings are concatenated, but I think that's a typical example and it was the setting I was thinking about to use format methods instead of string concatenation (and it's quite similar to many toString methods). I was surprised by the result:
String.format:757 ms
MessageFormat.format:1078 ms
String +:61 ms
String + (multi line):162 ms
StringBuilder:74 ms
Gosh! The "+" operator clearly is the winner! It is 10 (ten!) times faster then the format methods. 10 times, can you believe that. I was really surprised, and frankly, I couldn't believe it. (Update: daObi and Laurent explain it, see their comments below). Thus I changed the test, assuming the compiler tricked me, but I always got more or less the same results. I figured it may be the size of the strings, so I also changed that. That is, I used large strings (with 70.000 characters, the method returns one of two strings in order to avoid compiler optimization) in the concatenation:
  1. for (int i = 0; i < n; i++) {  
  2.     s = MessageFormat.format("Iteration {0,number,#},\nresult ''{1}'', ''{2}''", i,  
  3.    largeString(i), largeString(i+1));  
  4. }  
  5.   
  6. for (int i = 0; i < n; i++) {  
  7.  s =  "Iteration " + i + ",\nresult '" + largeString(i) + "', '" + largeString(i+1) + "'";  
  8. }  
Actually, the test is a little bit unfair, as in all cases at least the result string has to be created (i.e. also the format methods have to create strings), but at least the string is rather long. The result string s has a length of 140027 characters, so this is a pretty long string, isnt't? Here are the result of the long-string-test:
String.format:584 ms
MessageFormat.format:816 ms
String +:621 ms
String + (multi line):867 ms
StringBuilder:555 ms
Yeah! Now we have the order I expected in the first place: StringBuffer.append is the winner, followed by String.formatMessageFormat.format has a little problem and is even slower than the String concatenation. But compared to the result of the first test, all methods are at the same speed. So, what did I learn? First, I will use the plain and simple string concatenation in the future more often, as it is easier to write in many cases compared to some weird format strings. I will use String.format only if I probably need to translate my code, since this is easier to translate a format string than changing the code (if the order of the sentence components change in another language). And, of course, I will use StringBuilder if large strings have to be concatenated, but I think I use it less in toString-methods. And maybe I'm going to read a book about performance hacking -- do you have any recommendations? Anyway, this test also teaches me (again) that good performance is not (only) a question of single statements, but a question of good algorithms and good design in general. Note: Actually this is not the first time I was surprised by the result of a preformance test. I did a test once comparing the performance of different class library designs in case of a math library for matrices and vectors. I have documented the test in the LWJGL forum, and its results influenced the design of the GEF3D geometry package (see package documentation).


Posted by [czar]
,

생활 팁

카테고리 없음 2014. 5. 8. 16:47

http://mp.weixin.qq.com/mp/appmsg/show?__biz=MzA4OTEyMDgzMg%3D%3D&appmsgid=10012039&itemidx=3&sign=e601f1978365ef2b6cbc82cb8522666b&scene=1&from=singlemessage&isappinstalled=0#wechat_redirect


그들은 무엇 보았다. 두려워 임신되지 않을 것

2013년 12월 24일 자동차 임대 유한 공사 Hulunbeier 타겟 액션


1, 원래의 클립은 그렇게 사용할 수 있습니다



2 당신이 옷을 거는 곳이 없다 말하지 말라


3 데이터 케이블 나쁘지 마무리? 데이터 라인은 내부에 화장지 튜브에 잘 드라이브


4 유니버셜 삽 아



5 달걀 양파 라운드의 맛뿐만 아니라

 

6 일 저장 습기를 먹고


4분의 7 목욕 -

 

8 랩 걸레 당신의 머리와 함께 바닥 솔직하게 이동합니다

9 페인트는 배럴의 중간에 밧줄을 넣을 수 있습니다 때, 가장자리를 닦으



10. 못 박기는 손으로하지

11은이 스프, 이유 일을 오버 플로우하지 않는 것이라고 말했다. . . [양쪽 중간 부의 최대 값이 오버 플로우에 도달 할 수] (상기 ..)

 

12. 키보드의 먼지를 청소하는 메모를 사용하여 

13에는 배관, 잘 물을 취하지 -



14 소나기와 약간의 눈이 차단?

당신은 [주장 원칙을 통해 흡수 할 수있는 밤처럼 머리에 묶어 비닐 봉투에 약간의 식초를 부어라고 : 물 칼슘과 마그네슘 이온을 누릅니다 탄산 칼슘과 수산화 마그네슘 고체를 생산하는 데 오랜 시간이 지나면 해결됩니다,와 이 산 용액을 생성 할 수 있습니다]

 

15도까지 얻을 수있는 알람 시계를 설정? 방수에 : 당신이 ~ ~ ~ ~ 추신 잠을 확인하기 위해이 시도 

16. 국수를 넣어 곳이 걱정하지 않아도



또 엔터테인먼트 - 

17 여름 혜택 - 등 빠른 얼음 맥주, 콜라, : 냉장고에 젖은 조직을 붙이기 전에, 얼음이 빨리. 

18.보다 더 많은 기능을 바인더 클립. . . 많은 사람들이 ~ ~ ~ 알고 있어야합니다



19 스피커 조금. . . . 이 ~ 걱정하지 마세요로

 

20. 사랑하는 전화 요금은 지상에 넣어. . . . 고민? ~이 그것을 시도

 

21. 휙! 말할 것도없고 누구 시도 할 수 있습니다 - 구입이 노력만큼 좋은이없는 의류 클립, 폐기물 캔으로 만든 꽃 화분 22. . 23 전화 홀더 - 어떤 사진을 보여 ....... 
 


24 구 우산 재활용

 

시도 지루 (25) - Shushu 

26. 거대한 하나의 아

 

27이 이동 보낼 확실히 장미의 소수보다 더 많은,하지만 진심은 아니다? ? ? ? (가장 중요한 것은 옷을 해체, 더 버튼을 언급하지 않는, 아, 돈을 저장하는 것입니다? 시장이 살 ~ ~ ~ ~ 스타일의 많은처럼 아주 싼 모양에 직접)

[정말 좋은] 


귀하의 콘텐츠가 좋은 공유, 나는 가장 큰 힘에 살고

공개 마이크로 신호 : S823864543

공개 이차원 코드



보고서


Posted by [czar]
,

압력솥

카테고리 없음 2014. 4. 25. 22:46
압력

Posted by [czar]
,