Diary of a (not so) lazy Java Developer coping with Java SE, Jave EE, ORM, Spring and the idiosyncrasy of IT.
Search This Blog
Friday, January 10, 2014
On-Demand Exporting a Spring Bean via JMX
1) Define the bean to be exported
<bean id="myBean" class="...." />
2) Define a MBeanExporter
<bean id="mbeanExporter" class="org.springframework.jmx.export.MBeanExporter" />
2) Get the exporter
ApplicationContext applicationContext = ...
MBeanExporter beanExporter = (MBeanExporter) applicationContext.getBean("mbeanExporter");
3) Export the bean
beanExporter.registerManagedResource(applicationContext.getBean("myBean"),
ObjectName.getInstance("com.company:type=Users,name=User-A"));
4) Bean is now accessible via JMX
5) Unregister (if needed)
beanExporter.unregisterManagedResource (ObjectName.getInstance("com.company:type=Users,name=User-A"));
Wednesday, November 20, 2013
Exception handling with Spring RestTemplate
Exception handling with Spring RestTemplate
<T> ResponseEntity<String> doPost( String uri,
T request,
HttpStatus expectedStatus)
{
ResponseEntity<String> response = null;
try
{
response = _restTemplate.postForEntity(uri, request, String.class);
}
catch (HttpStatusCodeException e) // thrown by DefaultResponseErrorHandler
{
// Server Response: 40x , 50x
}
catch (ResourceAccessException e) // thrown by RestTemplate.doExecute
{
// I/O error, including marshaling
}
catch (Exception e)
{
// other error
}
return response;
}
Tuesday, September 24, 2013
A Littel Junit Helper: org.junit.Assume
By using the JUnit's AssumeXXX methods, tests can be run based on specific assumptions.
If the assumption is not met the test will not run but will be shown as passed.
As Explained by an example:
@Test
public void checkNtfsFileSystem() {
FileSystem fileSystem = FileSystems.getType()
assumeTrue(fileSystem == NTFS); // run test only if filesystem is NTFS
//
// do test
//
}
Monday, August 19, 2013
How to Stop Embedded Tomcat
The following lines stop Tomcat and release the used ports and resources:
tomcatServer.stop();
tomcatServer.destroy();
Thursday, July 18, 2013
Hazelcast Notes (Map.put and Mapstore.load; write-back and bulk persistence methods)
Remember if you have a map that is backed by database the calls to Map.put will issue
Mapstore.load as the put should return the old value.
You can use Map.set to circumvent this behavior; the same goes for remove and delete.
Also note, that for write-back persistence MapStore.store and MapStore.load methods might be used if there are only some items to persist.
Friday, May 24, 2013
Sucessfull Java Socket Send with Broken Connection
With Java sockets it's possible that the socket write method is successful even if the network is down.
When Java issues socket write the request is passed forward to the Windows socket send function. The documentation of send says that
TCP connection is open unless it's closed or terminated:The successful completion of a send function does not indicate that the data was successfully delivered and received to the recipient. This function only indicates the data was successfully sent.and an older version also says
If data for which the peer protocol has buffer space cannot be successfully transmitted within a reasonable length of time, the connection is considered broken and subsequent calls will fail with the error code set to WSAETIMEDOUT.
For some operating systems "successfully delivered" just means that the data was passed into a buffer in the network stack, which will handle the transfer.
- application calls socket close
- application crashes and OS closes the socket
- OS closes the socket due to an event (e.g restart)
So if the network becomes disconnected the connection remains open if there's no data sent; there's no way to detect network hiccups just by listening to incoming connections.
TCP usually provides keep-alive messages to detect network failures.
References:
Friday, May 3, 2013
Cacheable HTTPS response
Some browsers (e.g Internet Explorer) cache content even if accessed via HTTPS, thus if server returns sensitive information this may be retrieved by other users who have access to the browser.
To fix it, add the following headers into response:
Cache-control: no-store
Pragma: no-cache
More info here.
Subscribe to:
Posts (Atom)
