GridPulse

as stimulating as black coffee and just as hard to sleep after.

How to solve BindException: Address already in use: JVM_Bind in JBoss on Windows

leave a comment

 

Sometimes, you’re just having a wonderful day and suddenly the worst just happens:

[ServiceController] Problem starting service jboss:service=Naming
    java.rmi.server.ExportException: Port already in use: 1198; nested exception is:
    java.net.BindException: Address already in use: JVM_Bind
    at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:243)
    ............

The regular solution is to check if the port is actually used. So fire up a command prompt and netstat –aon.

If you find the port, see what process is using it and kill it ;) .

If you don’t see the port you can reserve it by following the information in KB812873 – http://support.microsoft.com/kb/812873.

My ReservedPorts entry contains:

1433-1434

1097-1200

4543-4545

That should fix it. The bad thing is that it requires a restart.

Written by Bogdan

October 28th, 2009 at 1:09 pm

Posted in Development, Java

Tagged with ,

NanoDI, a small .NET Dependency Injection container

leave a comment

Some time ago I worked on some projects using ASP.NET that were mostly ASPX with some specialized ASHX’s (c# behind the scenes).
The handlers just generated some graphs or exported Excel files, regular code monkey style, no architecture, no plan, just write it fast – quick dirty hack, quick buck – and I always thought that these guys that accept .NET inferior stuff deserve what they get.

As time flew by, I started to get a taste of what .NET is all about, luring me to the dark side.

engine1

Photo by B-tal

At the beginning I felt like “this is what evil must taste like”, but I quickly got accustomed to all the new things and now, I’m playing with .NET stuff again, mostly C#, trying to level my skills and having a lot of fun in the process. I built things like the quick and dirty multi font viewer, buggy and poor, mostly because nobody uses it.

Now, I wanted to start something bigger, and nicer (I won’t say what) and I felt that I needed to do it the right way, you know, MAINTAINABLE.
I started looking for a DI container and I started with Spring and Pico. I’m pretty accustomed to pico and I’ve been using Spring since version 1 so I though I’d give them a try. Wrong, strange, alien, weird, huge, EVIL.

For what I wanted to do it really seemed this way. How’s about something that is 10 megs big because spring and dependencies is 7 megs big. And I’m not even going to use most of it, I just want some plain dependency injection and maybe some tooling , like fast i18n.

Problem solved, evil destroyed
I found the solution! Why not have some fun AND learn the inner workings of C# and .Net? Why not build my own? So I did!

NanoDI is a small dependency injection container and tooling for .NET C# projects that are small or that do not need the complexity of bigger IoC solutions.

NanoDI’s goal is to be small, fast and clean.

Links:
Nano Dependency Injection home
NanoDI Ohloh.net project page
NanoDI Google Code project

The code is junky by my standards but I’m slowly refactoring as my c# skills get better. Next week I’m going to finish scoping and start working on proxies and interceptors. You can help too!

Have a taste, have fun!

Written by Bogdan

September 10th, 2009 at 11:50 pm

Transactional unit tests that support scopes using JUnit and Spring

one comment

Most of the unit tests that you write don’t need support for transactions and scopes, but if you ever want to test your web frontend code properly you’ll hit into this one.

Writing transactional unit test with JUnit, Spring and Hibernate is easy, what you want to do is:

- Add a transaction management bean in your test context:

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

- Make your test extend AbstractTransactionalJUnit4SpringContextTests
- Annotate you test class with @TransactionConfiguration and @Transactional

If your transaction demarcation is correct( surrounding your dao or service ) you should be done. This is the cleanest way that I found.

Everything until now is fine and dandy. The problem arises when one of the injected beans is bound to a scope. It can be any scope but for this example I’ll take the simplest scope – the ’session’ scope.

In a non-transactional situation this should be pretty straight forward, just implement Scope and using a ConfigurableBeanFactory register it. The fact that we are extending and that our context is automatically created really kills the simple path.

You’ll get a org.springframework.beans.factory.BeanCreationException with a nested “java.lang.IllegalStateException: No Scope registered for scope ’session’”.

But do not despair, there is simple solution available and it’s called CustomScopeConfigurer.

The fastest and cleanest way to use it is:
- Create a simple class that will be used as our scope manager. Let’s call it MockSessionScope.

code

- In your test context, create a CustomScopeConfigurer that will automatically register your scope on context creation:

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="session">
<bean class="com.gridpulse.xandria.translator.MockSessionScope"/>
</entry>
</map>
</property>
</bean>

Run your tests and you’re done!

Written by Bogdan

August 21st, 2009 at 4:45 pm

Multi Font Viewer on xandertools.com

one comment

Being a software developer sometimes involves more than just coding.
This time it was a logo and a splash screen I did a while ago and I had a problem – I couldn’t remember what font I used.

Of course, having a lot of fonts installed made it a nightmare.


twittshot

I googled a bit and found a lot of shareware that allowed me to view multiple fonts and compare. I didn’t use any of it. Instead I used a nice piece of software called Opcion.

It solved my problem, although it acted a little strange plus, it was Java. I can handle it because I’m a Java developer.. but can anybody else?

After finishing the work I decided I could do something about it. So I did.

It’s called Multi Font Viewer and it’s free.

You can download it from: the Multi Font Viewer page at xandertools.com.

If you want to take a look at the code or contribute visit the Multi Font Viewer Google Code project.

Best of luck and happy fonting!

Written by Bogdan

August 13th, 2009 at 7:04 pm

On refactoring and code reuse

leave a comment

Strolling through Google Reader, diagonally reading blog posts from the almost 1 hundred blogs that I tend to follow(1000+ unread ;) ) a phrase jump started my brain.

Not ever line of code can be reused. A lot of web development is about crafting a very specific solution. Localization, error handling, and coding conventions introduce challenges.

This is part of a post from one of the Mozilla blogs that I’m following, namely the blog of Austin King.

Now, this is as straight forward and clear as it is correct, not ever line of code can be reused. Putting it out of its initial context (web development that is) and bringing it into to context of enterprise applications it becomes pure evil, used as an excuse to re-invent a perfectly good wheel.

Puzzle
Photo by tcp909

Some advice:

  • Reuse as much as you can, extracting recurring pieces of code as methods
  • If common code spawns between projects, always extract it in a common library
  • Run a copy-paste detector such as CPD. Extract the code to methods.
  • Try to keep your methods under a fixed number of lines, let’s say 20. Doing this will force you to extract consistent pieces of code in separate methods, making them more re-usable
  • Try to maintain low cyclomatic complexity. Use a metrics package such as PMD or Panopticode. Refactor complex methods to smaller, less complex chunks of reusable code.
  • If you have the feeling that it’s the second time you’ve written a piece of code, search for the first one, because it’s usually true.

Now go and read a good book about this kind of stuff. I recommend The Productive Programmer by Neal Ford. It’s an excellent book for beginners but don’t fear – it’s superb even if you’re an experienced developer, I caught some nice Groovy tips that made the reading worth while, maybe you’ll find something interesting too.

Written by Bogdan

June 22nd, 2009 at 6:30 pm

Posted in Development, Java