Tuesday, 19 August 2014

How to check if a Java string is Null or empty

Method 1:

if(str != null && !str.isEmpty())
Be sure to use the parts of && in this order, because java will not proceed to evaluating the the second if the first part of && fails, thus ensuring you will not get a null pointer exception from str.isEmpty()if str is null.

Method 2:

You can use Apache commons-lang
StringUtils.isEmpty(String str) - Checks if a String is empty ("") or null.
or
StringUtils.isBlank(String str) - Checks if a String is whitespace, empty ("") or null.

JMX - Java Management Extensions (can be used to monitor Camel & ActiveMQ, servicemix etc)

JMX

Java Management Extensions (JMX) is a Java technology that supplies tools for managing and monitoring applications, system objects, devices (e.g. printers) and service oriented networks.

Architecture

JMX uses a 3-level architecture:

  1. The Probe level - also called the Instrumentation level - contains the probes (called MBeansinstrumenting the resources
  2. The Agent level, or MBeanServer - the core of JMX. It acts as an intermediary between the MBean and the applications.
  3. The Remote Management level enables remote applications to access the MBeanServer through connectors and adaptors. A connector provides full remote access to the MBeanServer API using various communication (RMIIIOPJMSWS-* …), while an adaptor adapts the API to another protocol (SNMP, …) or to Web-based GUI (HTML/HTTPWML/HTTP, …).
Applications can be generic consoles (such as JConsole[5] and MC4J[6]) or domain-specific (monitoring) applications. External applications can interact with the MBeans through the use of JMX connectors and protocol adapters. Connectors serve to connect an agent with a remote JMX-enabled management application. This form of communication involves a connector in the JMX agent and a connector client in the management application.
Protocol adapters provide a management view of the JMX agent through a given protocol. Management applications that connect to a protocol adapter are usually specific to the given protocol.

Camel JMX

Apache Camel has extensive support for JMX to allow you to monitor and control the Camel managed objects with a JMX client.

Using JMX to manage Apache Camel

By default, JMX instrumentation agent is enabled in Camel, which means that Camel runtime creates and registers MBean management objects with a MBeanServerinstance in the VM. This allows Camel users to instantly obtain insights into how Camel routes perform down to the individual processor level.
The supported types of management objects are endpointrouteservice, and processor. Some of these management objects also expose lifecycle operations in addition to performance counter attributes.

JMX Service URL

The default JMX Service URL has the format:
registryPort is the RMI registry port and the default value is 1099.
You can set the RMI registry port by system property.
-Dorg.apache.camel.jmx.rmiConnector.registryPort=<port number>
Or, by adding a jmxAgent element inside the camelContext element in Spring configuration:
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <jmxAgent id="agent" createConnector="true" registryPort="port number"/>
    ...
</camelContext>


Monitoring Camel using JMX

Using JConsole to monitor Camel

The CamelContext should appear in the list of local connections, if you are running JConsole on the same host as Camel.
To connect to a remote Camel instance, or if the local process does not show up, use Remote Process option, and enter an URL. Here is an example localhost URL:service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi/camel
Using the Apache Camel with JConsole




ActiveMQ JMX


Apache ActiveMQ has extensive support for JMX to allow you to monitor and control the behavior of the broker via the JMX MBeans.

You can run ActiveMQBrowser to monitor by connecting to 

"service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"


Extra


Note you connect to only "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi", then you can see everything that JMX can monitor: in my case I can see
servicemix 
camel 
ActiveMQ




Monday, 18 August 2014

IntelliJ IDEA unit test

1>Via native build in
Windows: Press Ctrl+Shift+T in the code editor.
Mac OS:  Cmd+Shift+T

2> via plugin JunitGenerator V2.0
打开IntelliJ IDEA工具,Alt+Ctrl+S,弹出窗口如下:
在文本框中输入Plugin进行插件搜索设置。
点击按钮,从插件资源库中安装新的插件。
从插件资源库中搜索JunitGenerator V2.0版本,在插件位置,鼠标右击

选择Download and Install ,在弹出的对话框中选择yes按钮,点击OK之后在需要重启下工具,选择Restart按钮,到此JunitGenerator2.0 插件安装完毕.
现在可通过此工具自动完成test类的生成了,在需要进行单元测试的类中Alt+Insert,


测试类中使用的相关注解跟代码如下:

[java] view plaincopy
  1. package test.RXTemplateService;  
  2.   
  3. import RXTemplateService.YhService;  
  4. import org.junit.After;  
  5. import org.junit.Before;  
  6. import org.junit.Test;  
  7. import org.junit.runner.RunWith;  
  8. import org.springframework.test.annotation.Rollback;  
  9. import org.springframework.test.context.ContextConfiguration;  
  10. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  11. import org.springframework.test.context.transaction.TransactionConfiguration;  
  12. import org.springframework.transaction.annotation.Transactional;  
  13.   
  14. import javax.annotation.Resource;  
  15.   
  16. /** 
  17.  * YhService Tester. 
  18.  * 
  19.  * @author <Authors name> 
  20.  * @version 1.0 
  21.  * @since <pre>���� 19, 2013</pre> 
  22.  */  
  23.   
  24. /*用于配置spring中测试的环境*/  
  25. @RunWith(SpringJUnit4ClassRunner.class)  
  26. /* 
  27. 用来指定加载的Spring配置文件的位置,会加载默认配置文件 
  28. @ContextConfiguration 注解有以下两个常用的属性: 
  29. locations:可以通过该属性手工指定 Spring 配置文件所在的位置,可以指定一个或多个 Spring 配置文件。 
  30. inheritLocations:是否要继承父测试用例类中的 Spring 配置文件,默认为 true。 
  31. */  
  32. @ContextConfiguration(locations = "classpath:test/RXTemplateService/applicationContext.xml")  
  33. /* 
  34. @TransactionConfiguration是配置事务情况的注解. 
  35. 第一个参数transactionManager是你在applicationContext.xml或bean.xml中定义的事务管理器的bean的id; 
  36. 第二个参数defaultRollback是表示测试完成后事务是否会滚 参数是布尔型的 默认就是true 但强烈建议写上true 
  37. */  
  38. @TransactionConfiguration(defaultRollback = true)  
  39. @Transactional  
  40. public class YhServiceTest {  
  41.     @Resource  
  42.     private YhService yhService;  
  43.   
  44.     @Before  
  45.     public void before() throws Exception {  
  46.     }  
  47.   
  48.     @After  
  49.     public void after() throws Exception {  
  50.     }  
  51.   
  52.     /** 
  53.      * Method: checkDlzhAndDlmm(String dlzh, String dlmm) 
  54.      */  
  55.     @Test  
  56.     public void testCheckDlzhAndDlmm() throws Exception {  
  57.         assert true : yhService.checkDlzhAndDlmm("wbb""wbb");  
  58.     }  
  59.   
  60.     /** 
  61.      * Method: resetMm(String xmm, Integer id) 
  62.      */  
  63.     @Test  
  64.     public void testResetMm() throws Exception {  
  65.         yhService.resetMm("admin"1);  
  66.     }  
  67.   
  68.     /** 
  69.      * Method: yhSave(T_XT_YH yh) 
  70.      */  
  71.     @Test  
  72.     @Rollback(false)  
  73.     public void testYhSave() throws Exception {  
  74. //TODO: Test goes here...  
  75.     }  
  76.   
  77.     /** 
  78.      * Method: yhDelete(String ids) 
  79.      */  
  80.     @Test  
  81.     public void testYhDelete() throws Exception {  
  82. //TODO: Test goes here...  
  83.     }  
  84.   
  85.     /** 
  86.      * Method: checkDlzh(String dlzh, Integer id) 
  87.      */  
  88.     @Test  
  89.     public void testCheckDlzh() throws Exception {  
  90. //TODO: Test goes here...  
  91.     }  
  92.   
  93.     /** 
  94.      * Method: findYhById(Integer id) 
  95.      */  
  96.     @Test  
  97.     public void testFindYhById() throws Exception {  
  98. //TODO: Test goes here...  
  99.     }  
  100.   
  101.     /** 
  102.      * Method: getYhList(int pageNo, int pageSize, Integer ssjgId) 
  103.      */  
  104.     @Test  
  105.     public void testGetYhList() throws Exception {  
  106. //TODO: Test goes here...  
  107.     }  
  108. }   

Total visitors since Jan 2012

World Visitor Map