Wednesday, October 17, 2012
Interesting articles
Since one of my aims of starting a blog was to treat it as a notepad or a storage place for some interesting concepts regarding programming I recommend reading these articles:
How to tune Java Garbage Collector
NoSQL week review
Javascript and jQuery
Tuesday, October 16, 2012
Calculating Coherence cluster size on a remote server using JMX
In one of the projects I had a problem regarding calculating the size of a Oracle Coherence cluster. It doesn't seem to difficult since there is already a nice piece of code that can do it for us:
Calculating Coherence cluster size
The problem is that this code is working properly only if the Coherence cluster is not remote. So how can we access it?
We have to modify the following function
public static MBeanServer getMBeanServer() {
MBeanServer server = null;
for (Object o : MBeanServerFactory.findMBeanServer(null)) {
server = (MBeanServer) o;
if (DOMAIN_DEFAULT.length() == 0 ||
server.getDefaultDomain().equals(DOMAIN_DEFAULT)) {
break;
}
server = null;
}
if (server == null) {
server = MBeanServerFactory.createMBeanServer(DOMAIN_DEFAULT);
}
return server;
}
To this one
public static MBeanServerConnection getMBeanServer() throws IOException {
JMXServiceURL url = new JMXServiceURL("service://...");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
return jmxc.getMBeanServerConnection();
}
And modify the line
MBeanServer server = getMBeanServer();
To use the new interface
MBeanServerConnection server = getMBeanServer();
More information on how to define the Coherence JMX service URL and JMX for Coherence as such can be found here
Managing Coherence using JMX
Sunday, October 7, 2012
Simulation of time consuming actions in integration tests
Hi!
Quite recently in one of my projects I had a situation in which I needed to create an integration test for the application. That's not very odd isn't it? :)
What was interesting was the fact that the logic of the app involved some concurrency issues and one of the components had to connect to an external service which would take a couple of seconds. Since in the integration test there was no need to make the actual connection, the component needed to be mocked. What about the simulation of the time consuming action? Well, let's take a look at the way I did it...
The task.
package pl.grzejszczak.marcin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Service that does some things including processing of the external service
*
* @author marcin
*
*/
public class SomeTask implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(SomeTask.class);
// Service is injected via a dependency injection system
private Processable timeConsumingExternalService;
private void methodThatConnectsToExternalServices() {
// connects to an external service and spends a couple of seconds there
LOGGER.debug("Before processing");
timeConsumingExternalService.process();
LOGGER.debug("After processing");
// some other things to do
}
public void run() {
methodThatConnectsToExternalServices();
}
public void setTimeConsumingExternalService(Processable timeConsumingExternalService) {
this.timeConsumingExternalService = timeConsumingExternalService;
}
}
The integration test.package pl.grzejszczak.marcin;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ServiceIntegrationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceIntegrationTest.class);
private ExecutorService executorService = Executors.newCachedThreadPool();
private Processable timeConsumingExternalServiceMock = Mockito.mock(Processable.class);
private SomeTask someTask = new SomeTask();
public ServiceIntegrationTest() {
initializeMocks();
}
private void initializeMocks() {
Mockito.doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) throws Throwable {
// Simulation of connection to external services
LOGGER.debug("Sleeping");
Thread.sleep(5000);
LOGGER.debug("Stopped Sleeping");
return null;
}
}).when(timeConsumingExternalServiceMock).process();
// Inject the mock to the Task - in any possible way
someTask.setTimeConsumingExternalService(timeConsumingExternalServiceMock);
}
public void executeTest() {
executorService.execute(someTask);
}
public static void main(String args[]) {
ServiceIntegrationTest integrationTest = new ServiceIntegrationTest();
integrationTest.executeTest();
}
}
And the output to the console:
2012-10-07 22:42:37,378 DEBUG pl.grzejszczak.marcin.SomeTask:21 Before processing
2012-10-07 22:42:37,389 DEBUG pl.grzejszczak.marcin.ServiceIntegrationTest:28 Sleeping
2012-10-07 22:42:42,390 DEBUG pl.grzejszczak.marcin.ServiceIntegrationTest:30 Stopped Sleeping
2012-10-07 22:42:42,392 DEBUG pl.grzejszczak.marcin.SomeTask:23 After processing
Let's take a closer look at the most important part in which an Answer for the execution of the service is being created
Mockito.doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) throws Throwable {
// Simulation of connection to external services
LOGGER.debug("Sleeping");
Thread.sleep(5000);
LOGGER.debug("Stopped Sleeping");
return null;
}
}).when(timeConsumingExternalServiceMock).process();
This piece of code changes the default action that should be done by the given object on a given method execution. In this particular case we had to mock a method that returns void - that's why we start with doAnswer(...) and finish with when(...).process().
That is how inside the integration test I managed to create a simulation of waiting for the service to finish. If you have any ideas or comments on how you would do it in another way please feel free to post a comment below :)
Saturday, October 6, 2012
And so we code...
Hi! My name is Marcin and I'm a software developer at Rule Financial. Since I'm a coding addict when I'm not coding in Java I'm coding in Java. In addition to that I'm developing websites (including Ryszard Grzejszczak's Lawyer's office, Center for Electoral Studies at University of Lodz, DF Private Financial Advisory). This blog will be about some of the issues that I found in my projects (the majority of those projects you can find on my website) and the ways that I delt with them.
So... To all the coding junkies out there...
So... To all the coding junkies out there...
Subscribe to:
Posts (Atom)