riahc3 50 Â Team Colleague

Hey

Since I couldnt get my web service to really support multiple users, Ive thought of simply implementing a FIFO kinda of queue like this:

(The code is made up on the spot right now. Therefore, ; may be missing, some variable declared wrong, etc. Ignore that. Just concentrate on the logic)

public class somename
{

private int users;

/*Alternative:*/
// private static int users;

public void somewebservice(String somevariable)
{

if (users>0)
{
      do
            {
                  system.out.println("please wait")
            }while (users>0);
}
users=1;
system.out.println("bla");
system.out.println("bla");
system.out.println("bla");
system.out.println("bla");
/*more code here*/
//End of code:
users=0;

}



}

Would this work when multiple users call the web services at the same time to make the queue?

Thanks

riahc3 50 Â Team Colleague

Oh well........I guess Ill have to try other sites.


Thank you anyways.

riahc3 50 Â Team Colleague

i'm aware of the concept of a pool, just never came into contact with that package or it's classes, I'm afraid.

This is the Apache Commons package.

Do you know any other, the "default" one with the JDK, or something alternative you are use to?

I may be able to modify my code to work with it....

riahc3 50 Â Team Colleague

as I said, I never worked with it before, I never even heard of it, so I'm not an expert on the matter, but maybe this can help you out?

Was it GenericObjectPool, ObjectPool or none that you heard about at all?

Thanks for all the help stultuske. I really appreciate it.

riahc3 50 Â Team Colleague

It fails on addObject() as well (same error)

riahc3 50 Â Team Colleague

I tired (one of the things mentioned) is addObject() before getting from the pool but that doesnt work either....

riahc3 50 Â Team Colleague

if the error message is the same, it's probably the borrowObject method allright. according to the api's, that can throw an Exception if there is no object that can be borrowed.

And then how can I add a object to that pool so it can be borrowed?

I read the APIs and it really doesnt give me any hint as to what it could be.

riahc3 50 Â Team Colleague

The error happens when I call the borrowObject method. Im sure because of the println right before it (that prints out) and the println after it (which does NOT print out)

riahc3 50 Â Team Colleague

There is nothing else I have changed except add that line.

riahc3 50 Â Team Colleague

BTW, that still doesnt work....

riahc3 50 Â Team Colleague

well, your problem is:

t = fechapool.borrowObject();

chances are, your fechapool is still null. could be your initialisation of fechapool is in another scope, or you've reset the value to null somewhere.

can't say for sure though

OK......

// ^^^ Code before
        //Fecha t=new Fecha(); //Old way
        Fecha t=null;
        try 
/*Line 127*/        {
            System.out.println("Intento cojer del pool de fecha"); //THIS IS PRINTED
/*Cant be null now right?*/ fechapool=new GenericObjectPool<Fecha>();
            t = fechapool.borrowObject();


            System.out.println("Ya he cojido del pool de fecha"); //THIS IS NOT PRINTED
        } 
        catch (NoSuchElementException e2) 
        {
            System.out.println(t.CojerTiempoAhoraMismo() + ": " + "Fallo en 'no such element' para el pool del objeto fecha " + e2.getLocalizedMessage());

        } 
        catch (IllegalStateException e2) 
        {
            System.out.println(t.CojerTiempoAhoraMismo() + ": " +"Fallo en 'illegal state exception' para el pool del objeto fecha " + e2.getLocalizedMessage());

        } 
        catch (Exception e2) 
        {
            System.out.println(t.CojerTiempoAhoraMismo() + ": " +"Fallo en 'exception' para el pool de fecha " + e2.getLocalizedMessage());

        }
riahc3 50 Â Team Colleague

And no, none of the excepctions are printed as you see........

riahc3 50 Â Team Colleague

At that line is where I attempt to get the object:

// ^^^ Code before
		//Fecha t=new Fecha(); //Old way
		Fecha t=null;
		try 
/*Line 127*/		{
			System.out.println("Intento cojer del pool de fecha"); //THIS IS PRINTED
			t = fechapool.borrowObject();
			System.out.println("Ya he cojido del pool de fecha"); //THIS IS NOT PRINTED
		} 
		catch (NoSuchElementException e2) 
		{
			System.out.println(t.CojerTiempoAhoraMismo() + ": " + "Fallo en 'no such element' para el pool del objeto fecha " + e2.getLocalizedMessage());
		
		} 
		catch (IllegalStateException e2) 
		{
			System.out.println(t.CojerTiempoAhoraMismo() + ": " +"Fallo en 'illegal state exception' para el pool del objeto fecha " + e2.getLocalizedMessage());
			
		} 
		catch (Exception e2) 
		{
			System.out.println(t.CojerTiempoAhoraMismo() + ": " +"Fallo en 'exception' para el pool de fecha " + e2.getLocalizedMessage());
			
		}
riahc3 50 Â Team Colleague

are you using a datospedidos class?

That is my main.

Pardon, my main is inside there.

Without ObjectPools, the code works correctly so there is nothing "strange" about it...

riahc3 50 Â Team Colleague

The error shown is this

[img]http://img838.imageshack.us/img838/2261/errorst.png[/img]

I tried private GenericObjectPool<Fecha> fechapool = new GenericObjectPool<Fecha>(); before you posted to see if it worked and this error comes up (similar to the one before, originally in the thread)

riahc3 50 Â Team Colleague

found a few mistakes:

I have a stupid way of just writing code instead of copy/paste so that open bracket is closed in my code.

How do you instantiate fechapool? Like I said, havent worked with ObjectPools before...

Thanks for the help

riahc3 50 Â Team Colleague

I removed the pool code to the original state of the program to see if I didnt break anything along the way; The reason I cant give you the exact message but Im pretty sure it was that.

It didnt say anything explict that gave any information at all.......

riahc3 50 Â Team Colleague

are you getting any error messages?

Yes.........I think it was invalid calling the native method function.

riahc3 50 Â Team Colleague

how do you try to run it? what's in your main method? what error message do you get?
if you are trying to run one of the above classes, it will fail because there is no main method in them.

My main function calls somefunction(). "Im going to set set actives" does get displayed so that part works. As soon as setmaxactive is modified or borrowobject is called, it fails.

riahc3 50 Â Team Colleague

Hey I have this code:

This is my main part of my program:

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;

import Fecha;



public class someclass
{

private GenericObjectPool<Fecha> fechapool;

	public boolean somefunction()
         {
              System.out.println("Im going to set set actives");
              fechapool.setMaxActive(5);
              System.out.println("set actives sets");

         }
{

It doest get to the "set actives sets" println. It prints out a Java error.

Here is my Fecha class

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class Fecha 
{
	
	public String ParsearFecha(String fecha,String formato)
	{
		formato=formato.toLowerCase();
	         System.out.println(fecha);
		
		String[] trozoTotal = fecha.split(" ");
		String[] trozoFecha = trozoTotal[0].split("-");
		String[] trozoHora = trozoTotal[2].split(":");
		String trozoSeg = trozoHora[2].substring(0, trozoHora[2].indexOf("."));
		String trozoMil = trozoHora[2].substring(trozoHora[2].indexOf(".") + 1);

	
		if (fecha.equals(null))
		{
			return null;
		}
		
		
		if ((formato.equals("año")) || (formato.equals("anio")))
		{
			return trozoFecha[0];
		}
		else if (formato.equals("mes"))
		{
			return trozoFecha[1];
		}
		else if (formato.equals("dia"))
		{
			return trozoFecha[2];
		}
		else if (formato.equals("diasemana"))
		{
			return trozoTotal[1].toUpperCase();
		}
		else if (formato.equals("horas"))
		{
			return trozoHora[0];
		}
		else if (formato.equals("minutos"))
		{
			return trozoHora[1];
		}
		else if (formato.equals("segundos"))
		{
			return trozoSeg;
		}
		else if (formato.equals("milisegundos"))
		{
			return trozoMil;
		}
		else if (formato.equals(null))
		{
			return trozoFecha[0]+"-"+trozoFecha[1]+"-"+trozoFecha[2]+" "+trozoTotal[1].toUpperCase()+" "+trozoHora[0]+":"+trozoHora[1]+":"+trozoSeg+"."+trozoMil;
		}
		else
		{
			return trozoFecha[0]+"-"+trozoFecha[1]+"-"+trozoFecha[2]+" "+trozoTotal[1].toUpperCase()+" "+trozoHora[0]+":"+trozoHora[1]+":"+trozoSeg+"."+trozoMil;
		}
		
	
	}
	
	public String CojerTiempoAhoraMismo()
	{	
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd EEE HH:mm:ss.SSS",Locale.getDefault());
		Calendar cal = Calendar.getInstance();
		return dateFormat.format(cal.getTime()).toUpperCase();
	}

}

As you see Fecha is not really complex so, not sure what it happening so if someone could explain.

When using ObjectPool and borrowObject(), it also fails there.


Any ideas/tips?

riahc3 50 Â Team Colleague

Hey

Is there a way I can print out the line Im currently on


Example:

public class something
{

public void somefunction()
{
System.out.println("I am line number + " /*the number 6 would appear here*/);
System.out.println("I am line number + " /*the number 7 would appear here*/);

System.out.println("I am line number + " /*the number 9 would appear here*/);
}


}

Thank you

riahc3 50 Â Team Colleague

No help or ideas?

riahc3 50 Â Team Colleague

You may be right. But a loop like that will be incredibly fast, and creating a new object and garbage collecting the old one will have some overhead, so it's not obvious which would be faster. You would have to benchmark both to see what happens in reality. Either way it's going to be some tiny fraction of a second, certainly too fast for a human to see.

Noted. Thanks.

riahc3 50 Â Team Colleague

Wow, noone else had to say thanks to anyone on this forum?

Amazing how ungreatful people are....

riahc3 50 Â Team Colleague

Nothing at all?

riahc3 50 Â Team Colleague

I like Kentico from what it looks like but going all Sharepoint might also intrest me (as all the tools are integrated).

The only other expirence I have with CMS is Magento.

riahc3 50 Â Team Colleague

Hey

Im trying to make a social network (local based, not anything to take on other social networks) but Im not going to reinvent the wheel and make everything from scratch. Thats why Im asking if there is any CMS/library/etc to make my life easier.

Im going to use C#/ASP.NET as personally I like the .NET framework and this will give me expirence with it. Also, I dont play to put any money into the project so lets stick for now with free CMS. Of course, any paid recommendations will be considered.

The idea Id which to keep private but it is a "mouth to mouth" sort of site where things like mentioning another user will be important.

If someone could give me some CMS ideas, thanks.

(BTW, if the CMS makes designing easier, GREAT! Im not a designer, Im a programmer so design means nothing to me :P )

riahc3 50 Â Team Colleague

Hey

I wanted to make a thread for not only myself but for others to give a "thank you" to anyone who has helped them.

In my case, without some suggestions here, I would have been stuck for days on a single thing and not be able to advance further.

I got a month left at my job so more Java things will problably pop up :P but meanwhile Im trying to get a C# social network going so might pop up over there more frequently. But who knows....

Anyways thanks to all :)

riahc3 50 Â Team Colleague

Whether you clear all the elements back to zero, or dereference the array then create a new one is your choice. In practice it won't make the slightest difference unless the array is so large (millions of elements) that you are running short of memory.

There is the reason why I think dereferencing them makes a difference: The array is (I believe) 50000 going thru all the positions and setting them to 0 (some of the positions are never used, justed declared for extra space) is stupid. Thats why I think the null method is better.

riahc3 50 Â Team Colleague

If you put it like that then setting the value to null would be your answer but you are not responsible for destroying objects. When you deference an object by setting the reference to null if there are other references to it then that object becomes available for garbage collection.

The garbage collector (GC) is responsible for freeing allocated memory and you are not in control of when the GC runs.

So you need not worry to much about resetting object state (Java takes care of that for you).

only C++ programmers need worry

It is a web service so I believe I do; I reutilizes the objects (declaring instances of them) at the beginning of the code and I destroy them at the end (with this null fix)

riahc3 50 Â Team Colleague

A would only work for an array of ints. And what do you mean by resetting (u need to provide some context)

I mean by resetting by leaving it as it originally was when I first declared it.

(Or destroying the object/instance of the object itself would also be valid)

riahc3 50 Â Team Colleague

Hey

I want to "reset" a array....What would be the correct way to do it?

Code A:

for (int i=0;i<=somearray.length;i++)
{
 somearray[i]=0;
}

or Code B:

somearray=null;

Thank you

riahc3 50 Â Team Colleague

Hey

I want to open the Apache Tomcat server that is installed in MyEclipse but without having to open MyEclipse; as a standalone application of sorts. How do I do this?


Thank you

riahc3 50 Â Team Colleague

I'm using Java 6 but I solved it: had to set the file type to binary instead of the default of ASCII. My fault.

dantinkakkar commented: In every thread of yours, you find the solution at the very end yourself!. :P +4
riahc3 50 Â Team Colleague

Can you explain what the fix was?

I don't like sloppy fixes so that's why I didn't post it.

On the local drive, I make a copy of the file inside the folder and that copy I transfer to the FTP server which works

riahc3 50 Â Team Colleague

Well I just tried it and I got this error:

(Generated from a exception)

java.lang.ArrayIndexOutOfBoundsException: -1

This never happens when calling it just as one.

riahc3 50 Â Team Colleague

Hey

I am having problems making a exact copy of a file. It seems that it changes the headers of the file.

I tried using the Apache fileutils copyfile method but it seems to change the header completely. The rest of the file looks pretty much OK.

I think it works but the problem is that the FTP transfer fails.......It generates the copy correctly I think


Any way to do it?

riahc3 50 Â Team Colleague

Using a very sloppy fix :P I was able to do it..........but now I have another problem......


(New thread)

riahc3 50 Â Team Colleague

In another project (Where I created the same structure) it doesnt work at all.

riahc3 50 Â Team Colleague

All of the FTP commands should receive a response with a response code. I don't see in your code where you ever check the response code for error conditions. What if the server finds a problem or there is an error, what would/should your code do?

The Apache library is ready and prepared for response codes returned from the server so the library would throw a (Java) exception if something went wrong. That's one of the reason that the change working directory returns a boolean.

Today (in 30 mins) I'm going to try to do it in a another project and see how it acts. Let's see if breaking down this program, we can see where the problem is. Ill post when I have the result.


Thank you for your help

riahc3 50 Â Team Colleague

Have you looked on the server to see if the file is being written in another location.

Yes and I cant find it anywhere.

Which is strange as no error is given which means the file has been transfered (correct?)

It also replies things like when I use the change current directory command (thru Java and the Apache library) if the folder exists or not.

Im going to sleep and lets see if tommorow at my job I can give you more information.

riahc3 50 Â Team Colleague

Have you tried doing the FTP commands manually to see how to use them?

I believe I tried in the beginning when I was messing around with this FTP code (way back, 3-5 months ago) tried FTP commands but the server didnt like it, it seems.

BTW, it is running Windows CE if that helps.

riahc3 50 Â Team Colleague

If you can't send one, how can you expect to send more than one?
Can you create a test program that can send one file? When you get that to work, then work on sending more than one file.

I was hoping that since I could make at least the folder with the filename, I would be able to send a file as normal...

riahc3 50 Â Team Colleague

Which side do you think the problem is on: client or server?
Can you send one file successfully?
Is the problem only when you try to send more than one file?

I think it is a client side problem as the server correctly makes the folder.
"Yes" but with " " since it creates a folder with the name of the value in the variable fichero.
No, I cant send one. It only (for now) does what is stated above.

riahc3 50 Â Team Colleague

What is the best way to start/stop/restart Apache Tomcat using Java code? If it cant be platform dependent (or complicated or I have to do a "IF" for each), then only for Windows.

Thanks

riahc3 50 Â Team Colleague

Back at work....

package herramientas;


import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;

Im currently with those packages and imports.......

riahc3 50 Â Team Colleague

What packages are you using? Where do the classes come from?

What more does the server do or not do? Are there error messages on the server?

What I use in order to do the FTP transfers is the Apache commons library.

The server gives no error at all. The server like I mentioned creates a empty folder with the value in the varible of fichero being its name.

riahc3 50 Â Team Colleague

(Lunch break, in case anyone asks for more code)

What does the FTP server do when your program connects to it and sends it instructions and data? Are there any error messages?

What packages are you classes in? You don't show the import statements for the code.

It creates a folder (the FTP server) with the name of the fichero variable.

The imports were basically give ins, that's why I did not post them.

I left the PC back so I can VNC back to it if it is neccesary but I'm on a phone so....

riahc3 50 Â Team Colleague

Hey

I have a FTP server with this


ftp://192.168.100.2/folder/

And I have something like


anotherfolder:
afolderinsidethatone:
file1.bin
file2.bin
file.txt

I want to pass all of that to the ftp server so it takes this form:


ftp://192.168.100.2/folder/anotherfolder/
ftp://192.168.100.2/folder/anotherfolder/afolderinsidethatone/
ftp://192.168.100.2/folder/anotherfolder/afolderinsidethatone/file1.bin
ftp://192.168.100.2/folder/anotherfolder/afolderinsidethatone/file2.bin
ftp://192.168.100.2/folder/anotherfolder/file.txt

I have this code:

public void subidaftp(String ip, String usuario, String pass, String fichero, String directorio)
	{
		FTPClient client = new FTPClient();
		FileInputStream fis = null;
		try 
		{
			client.connect(ip);
			client.login(usuario, pass);
			String filename = fichero;
			fis = new FileInputStream(filename);		
			
			boolean existe=client.changeWorkingDirectory(directorio);
			if (existe==false)
			{
				client.makeDirectory(directorio);
				client.storeFile(filename, fis);
				client.rename(filename, directorio + filename);
			}
			else
			{
				client.storeFile(filename, fis);
				client.rename(filename, directorio + filename);
			}
			client.logout();
									
		} 
		catch (IOException e) 
		{
			System.out.println("ERROR: Ha ocurrido un error ( " +e.getLocalizedMessage()+" )");
		}
		finally
		{
			try
			{
				if (fis!=null)
				{
					fis.close();
				}
				client.disconnect();
			}
			catch (IOException e) 
			{
				System.out.println("ERROR: Ha ocurrido un error al cerrar el fichero ( " +e.getLocalizedMessage()+" )");
			}
		}
	}

And when I call it, there is no error message (so it finds the file I try to load) but It just creates a empty folder, nothing else.


How can I adapt it to just naming a folder and putting everything in the server keeping the structure?

Thanks

riahc3 50 Â Team Colleague

Noone has any idea what could be happening?