Category Archives: Uncategorized

A simple NULL killer.

You know what it’s like – you want to avoid nulls. Well this is part of my utilclasses package. Along with the “optional” package. This simple routine is a one line call to ensure I never get a null back.

To call It I simply do

String stringTest=null;
Integer numTest=null;
System.out.println(noNulls.fix(stringTest));
System.out.println(noNulls.fix(numTest));

Here’s the class.

package utilclasses;
/**
 * 
 * Another fine Java Class
 * http://www.netbeansboy.org
 * @author paulc
 */
public abstract class noNulls {

public final static Object fix(Object value) {
 if (value==null) {
     if (value instanceof String) return "";
     return 0;
     }
 return value;
 }
 
}

Setting up MON for mysql

Just a short post. I was trying to get two database servers to check each other for the running database. Here are the Parameters (I actually had to look at the package source to get them!).

msql-mysql.monitor --mode MySQL --database=dbname --user=user --password=pw

Tungsten Replicator setting up and Gotchas

Tungsten Replicator site Clicky

So we have a big MYSQL server with mission critical data on it.

I want to replicate it accross to another MYSQL database, they are both ubuntu servers with Percona MYSQL database.

1. Preparation

Master – Install RUBY, JAVA,

Slave – Install RUBY, JAVA

edit the hosts files on both machines in etc/hosts and add the other servers in each one and reference the IP address – just makes things easier.

2. SSH

Make sure you set up SSH so that you can ssh from one server to the other WITHOUT using passwords.

3. PORTS

make sure all ports required 10000,10001 are available (particularly problematical PLESK, WEBMIN a whole host of things like these ports) on all machines. One server actually had a PLESK tool on it even though plesk was not installed!!

4. MYSQL

make sure everything is set up and restore a backup of the database before you start.

5. Helper scripts

Set up your tungsten. You’ll need to edit NODES_MASTER_SLAVE.sh to make sure your nodes are correct and USER_CONFIG as per the tungsten documentation.

install.sh – put in the tungsten folder e.g. tungsten-replicator-2.1.0-343

VERBOSE=1 ./cookbook/install_master_slave ./cookbook/NODES_MASTER_SLAVE.sh

tungsten.sh (actually I renamed this to tungsten and did a chmod +x tungsten)

if [ "$1" == "help" ] || [ "$1" == "" ]; then
 echo "Commands"
 ls installs/cookbook/tungsten/tungsten-replicator/bin/
else
installs/cookbook/tungsten/tungsten-replicator/bin/$1 $2 $3 $4 $5
fi

So you can type

./tungsten replicator status

To run commands.

Building a processing application with EJB’s (don’t use THREADS!)

I have for some time had to build processing applications. These normally consist of thread based things that run off and do a lot of calculations and database work. These days however I use EJB’s and the @Asynchrounous tag above a routine to make it work asynchronously.

To make it work you need to create 2 stateless session beans.

One will hold your routines that you wish to run, lets call this runnerBean, you need to create a local interface to it.

Second, create the timer bean that will start off the threads in runner bean.

In the timerBean that starts all the threads off create an @Schedule routine and a reference to the runnerBean.

@EJB
private doRunnerLocal doRunner;

@Schedule(second = “*/5”, minute = “*”, hour = “*”, persistent = false)
private void importThreadManager() {

while (true) {

List serialNos=new ArrayList();

try {
serialNos.em.createNativeQuery(“select * from serialnos where processed<1”,SerialNos.class).getResultList();
} catch (Exception e) {

}

if (serialNos.isEmpty()) {
// Have a sleep for a while if nothing found
try {
Thread.sleep(10000);
} catch (InterruptedException x) {
}
} else {
for (i=0;i<serialNos.size();++i) {
doRunner.doSomething((String)serialNos.get(i));
}
}

}

In the runner bean then create your Async threads really simply.

@Asynchronous
public void doSomthing(String serialNo) {

// Process serial numbers

}

Job Done!

Starting a glassfish server on a VPS, solving Memory Problems.

1. Check your hosts file – if your glassfish server constantly says ports are occupied make sure

/etc/hosts

the localhost entries are first

2. Make sure your host is called localhost

hostname localhost

3. Solve memory problems

export _JAVA_OPTIONS=”-Xms20m -Xmx64m -XX:MinHeapFreeRatio=20 -XX:MaxHeapFreeRatio=40 -XX:NewSize=10m -XX:MaxNewSize=10m -XX:SurvivorRatio=6 -XX:TargetSurvivorRatio=80 -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled”


before you start your server.

Using threading to do processing. Improving the User Experience.

One great thing about Java is that it’s easy to create Threads. My UML diagrams are showing how I am now splitting processing away from the user interface.

Why make a user wait, when a background thread could be fired to do a calculation, produce a report and it’s not only users that can benefit from background processing. I have a comms routine which basically get’s a UDP message, puts it in a database and responds to the unit. Then, in the background, runs a thread that processes the data into the system. 

To create a thread is very easy. You can either use an EJB timer function to start a threaded routine, or create a class that extends a Thread, and then use new Thread(new yourClass()).start();

 

Sending a text through txtlocal.com (or how to do a post to a page)

The java examples they provide require the use of an apache library that has superseded. So I wrote my own using simple java libraries. No extras needed.

Obviously you’ll have to get yourself a txt local free text account before you go ahead….

http://www.txtlocal.co.uk/?tlrx=89707

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package phj;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/**
 *
 * @author paulc
 */
public class SMSSender {



    public String sendSms(String sToPhoneNo,String sMessage) {

    try {
    // Construct data
    String data = URLEncoder.encode("uname", "UTF-8") + "=" + URLEncoder.encode("txtlocalusername****", "UTF-8");
    data += "&" + URLEncoder.encode("pword", "UTF-8") + "=" + URLEncoder.encode("txtlocalpassword****", "UTF-8");
    data += "&" + URLEncoder.encode("message", "UTF-8") + "=" + URLEncoder.encode(sMessage, "UTF-8");
    data += "&" + URLEncoder.encode("from", "UTF-8") + "=" + URLEncoder.encode("from email address", "UTF-8");
    data += "&" + URLEncoder.encode("selectednums", "UTF-8") + "=" + URLEncoder.encode(sToPhoneNo, "UTF-8");
    data += "&" + URLEncoder.encode("info", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8");
    // Send data
    URL url = new URL("https://www.txtlocal.com/sendsmspost.php");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    String sResult="";
    while ((line = rd.readLine()) != null) {
        // Process line...
        sResult=sResult+line+" ";
    }
    wr.close();
    rd.close();
    return sResult;
} catch (Exception e) {
    System.out.println("Error SMS "+e);
    return "Error "+e;
}

    }
}

Obviously mine is more complex now (hence not final/static), just replace username, password and from email.

 

Rioting and Looting in the UK, what about software rioting?

Recently we have been witness to some terrible rioting and looting in the UK. People breaking in to shops. These people have broken lives and livelihoods and really, no one is sure why. Sure, there was the killing of a man by police. But a lot of the youth seem to have no idea why they did what they did and through our judicial system they will, rightly so, receive punishment.

But, for years, and every day there is a form of looting going on that affects livelihoods of people like me. I was 16 when I first had my own software company, I was young and immature but I worked day and night on the software  I released it was my passion. I realised that people had low moral values when I was offered a PIRATE COPY of my own software.

Not everyone in IT is rolling in it, not every company is making a fortune, when people rip of software it is like walking into a shop, picking up something that doesn’t belong to you and walking out with it. You don’t feel the effects of it, but we, in the software world do.

In a way that’s why I like the apple iTunes store for publishing my iPad / iphone/ mac apps. Because it’s very difficult to rip the software off, not without doing dirty things to your apple gear, which I for one don’t want to do.

So, people, before you wind up your torrent downloader, or head off to pirate bay, remember us software engineers, working hard, developing and inventing. Remember we have to feed our kids. We don’t come and steel your stuff from under your nose, don’t do it to us.

Netbeans or IntelliJidea for Android Development?

I’ve recently been developing some interesting apps for the Android platform, initially I wanted to use Netbeans (well, the blog’s NETBEANSBOY isn’t’ it). But then I found IntelliJIdea has been launched as an open source version!

It’s pretty good and ran the app pretty well. Still I will be sticking with Netbeans for everything else. The only problem I did find with IntellijIdea was that it didn’t much like sub versioning things. Mind you Netbeans in the past has had a problem with Subversion on linux where it would use the wrong version and then hang mid committing.

Good Job IntelliJIdea. Nice to see you join the open source world!