• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Using JMS message service point-to-point in a Java program

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


question 1) how does one embed this code in a large java program at the point where messaging is required ?

question 2) for multiple queues does one change the "queue/queue0" to "queue/queue1" etc.

Bob M
 
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bob Matthews wrote:question 1) how does one embed this code in a large java program at the point where messaging is required ?



I understood from your other thread that you planned to send messages infrequently (e.g. one per day). So you could change the name of the method and have your program call that method. If you were sending a lot of messages you would want to keep the connection open, though.

question 2) for multiple queues does one change the "queue/queue0" to "queue/queue1" etc.



That "queue/queue0" is the name of the queue you want to send the message on. So yeah, if you want to use a different queue you'd have to use that other queue's name.
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again

please confirm that in line 34 of the example code queueSender should read queSender

a similar error is to be found in the Receiver code

I still need advice on the code required by the java programs running on Computer 1 (the master computer) at the point where they are waiting to receive a message

How do you code it so that there is a wait until the message is received before continuing with the rest of the code ?

Bob M
 
Paul Clapham
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bob Matthews wrote:please confirm that in line 34 of the example code queueSender should read queSender

a similar error is to be found in the Receiver code



Yeah, those are errors. Did you write the code yourself or did you get it from somewhere else?

I still need advice on the code required by the java programs running on Computer 1 (the master computer) at the point where they are waiting to receive a message

How do you code it so that there is a wait until the message is received before continuing with the rest of the code ?



You write some code which receives a message. (It sounds like you already have some code like that.) If there are messages already in the queue, the code gets the first one and continues. If there aren't any messages, the code waits until there is one, then it gets that message and continues.
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul

I got the code from https://howtodoinjava.com/jms/jms-point-to-point-message-example/



The above is what I have at the moment............................
I take it from your comments that I don't need to worry about any code for waiting for response

I shall test this next Monday

Many thanks

Bob M
 
Paul Clapham
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am less than impressed by tutorials which include code examples which won't compile. But anyway, good luck with your testing!
 
Paul Clapham
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, what JMS provider have you chosen to use?
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul

Unsure which provider ???

All I have done so far is to

a) include the import statements as per example code

b) include the codes for the Sender and Receiver in the appropriate java programs

and compiled same OK

Am I missing something here ?

Bob M
 
Saloon Keeper
Posts: 5707
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JMS is just an interface - it needs a messaging provider to work. See https://coderanch.com/wiki/660091/Java-Enterprise-Edition-FAQ for some options.

I still think socket communication would be an easier solution, with less overhead, and very likely faster.
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim

Thanks for your input..................

OK, silly question then..........

If I adapt two example codes for both a client and a server, is that all I need to do ?

Bob M

p.s. having just looked at example code, where IP addresses and Ports are mentioned

I have multiple java programs running on both machines - so how does a message from one computer get to be received by the correct program on the other machine ?
 
Tim Moores
Saloon Keeper
Posts: 5707
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If I adapt two example codes for both a client and a server, is that all I need to do?


In your scenario, where it'll always be just one machine that initiates the connection, and the other receiving it, yes.

I have multiple java programs running on both machines - so how does a message from one computer get to be received by the correct program on the other machine?


That's what the port number is for - whichever Java code opens a ServerSocket for port X will get connections requests from other machines trying to connect to that specific port.
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim..................

Appears to be relatively easy to apply

Understand about port numbers now

will let you know how I get on with testing

Bob M
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim

I have added code for both server and client into my java programs and compiled OK

BUT one line from the client code is



I understand that this line returns both host name and host IP Address

As my 'host' computer is a different computer to the 'client' computer I am unsure how to modify this one line ?

Bob M
 
Tim Moores
Saloon Keeper
Posts: 5707
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is that address used for - to connect to the other host? Then something like InetAddress.getByName("1.2.3.4") is probably correct, where 1.2.3.4 is the IP address of the other host.
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim

Compiled OK

Looking forward to testing

Bob M
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim........nearly there



Two questions....................

1) how do I change the code in the SocketClient to setup the message I wish to send
e.g. message = "GBPAUD: " + Trading_Decision
where Trading_Decision is a known variable

2) The corresponding java programs run on both computers hourly
Do I have a timing problem if either the SocketServer starts first or the SocketClient starts first

many thanks

Bob M
 
Tim Moores
Saloon Keeper
Posts: 5707
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

how do I change the code in the SocketClient to setup the message I wish to send
e.g. message = "GBPAUD: " + Trading_Decision
where Trading_Decision is a known variable


In lines 52 and 53 it would write whatever string is appropriate. The for loop should not be necessary - both sides would just quit after a successful transmission has happened.

The corresponding java programs run on both computers hourly
Do I have a timing problem if either the SocketServer starts first or the SocketClient starts first


The server is designed to run all the time (hence the while loop). If you don't want that you can start it maybe 5 minutes before the hour, and then have it quit after a connection has been made. It would be more difficult to change the client to wait until the server is available.
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim

changes made to code............



The theory is that after a message has been received by Computer 1 from Computer 2
I will have Trading Decision 1 from Computer 1 and Trading Decision 2 (= message) from Computer 2
I then concatenate the two decisions and check for substrings like "LONG", "SHORT" etc to make a FINAL trading decision

Bob M
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Have tested the socket connection

Result: nothing happens

Code which calls the socket connection in both programs (client and server) seems to be being ignored

Does my calling routine look OK ?

Both PCs are connected by ethernet cable to the same modem

Bob M
 
Tim Moores
Saloon Keeper
Posts: 5707
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does "stop" mean it exits with an exception? I don't see how they could terminate without one. Write the exception stack traces to where you'll see it (ex.printStackTrace()) and post them here.
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK...will do

Bob M
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very confused now.....................

I see no sign of a Stack Trace anywhere ??

Bob M
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Any advice on where to go from here.......................

Computer 2: the call to Socket Client is the last piece of code
Computer 1: code following the call to Socket Server places actual trades and this still is still working

thus, my statement that the 2 calls seem to be being ignored.....................

Should I move over to Netbeans and simply run just the Socket Client and Socket Server codes by themselves ?

Bob M
 
Saloon Keeper
Posts: 2656
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if this helps -- I tried your code with some minor changes and it seems to be working.

SocketServer console:
SocketClient console:
SocketServer:
SocketClient
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ron for testing..................

One point:- I cannot compile successfully with the word 'static' in both Client and Server codes

Error message (on Server): Cannot make a static reference to the non-static field server

Does this highlight my problem ?

Bob M
 
Ron McLeod
Saloon Keeper
Posts: 2656
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bob Matthews wrote:Error message (on Server): Cannot make a static reference to the non-static field server


Which line of code is causing this error?
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Server Code - Lines 17 and 20

Client Code - Lines 13 and 28

Bob M
 
Paul Clapham
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this FAQ page: https://coderanch.com/wiki/660020/Main-Pain

When you've finished, apply the knowledge you've gained to that code.
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul

Thanks for your suggestion....BUT.................

we have instructions- First, Next, Next and finally

The second next instruction says "...in a different .java file" BUT I have just the one .java file ???
and so I don't understand

Bob M
 
Tim Moores
Saloon Keeper
Posts: 5707
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The second next instruction says "...in a different .java file" BUT I have just the one .java file ?


How is this related to the original problem? if this is a different problem, it would be better discussed in a separate topic.

But if it is still the same problem: Surely you have more than one Java file, SocketClient.java and SocketServer.java, no?
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim

I am running a single huge .java program on the Dukascopy Trading platform on each computer for a particular currency pair

Computer 1 (server) has the SocketServer code [I am 72yoa and call this a subroutine]
Computer 2 (client) has the SocketClient code

I am wishing to call the SocketServer code on Computer 1 [i.e. call a subroutine from within the main program]
I am wishing to call the SocketClient code on Computer 2

Still same problem: code not being actioned in any way (i.e. computers not communicating)

Bob M

 
Tim Moores
Saloon Keeper
Posts: 5707
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

a single huge .java program


If by this you mean that all the code is part of just one Java class, then the very first step should be to break it down into multiple classes.


I am wishing to call the SocketServer code on Computer 1 [i.e. call a subroutine from within the main program]
I am wishing to call the SocketClient code on Computer 2


Yeah, I got that. If you don't want to run the server all the time, then you should break it out of the main code and start it with cron when appropriate. Of course, if it's part of a lrger code that runs perpetually, then you might just as well have the server run as part of that code, and run it all the time as well.

Have you tried to run the code Ron posted? Since that seems to work, I think it might be a good next step to get it to run for you as well.
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Tim

One java program (1700 lines) on each computer
One program slightly longer since it trades

One class (to access Derby databases)
Another class (to write to File)
and the Socket Server class (or the Socket Client class - depending on which computer)

and several methods

both computers are running 24/7
java program on each is running 24/7 and being activated hourly

Bob M
 
Tim Moores
Saloon Keeper
Posts: 5707
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK.

Have you tried to run the code Ron posted? Since that seems to work, I think it might be a good next step to get it to run for you as well.

 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I haven't tried it...................

Point being I am quite sure it works as a stand alone exercise

My problem is how to incorporate the code into my java program

I am trying to follow Paul's suggestion but get hung up on the 2nd next instruction as I said above

Bob M
 
Rancher
Posts: 460
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check out the java networking tutorials yet?  I think they would help maybe.
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Al

Yes I have looked at the tutorials............

However sometimes it is difficult for me to take a stand-alone example that works (as we have here) and change it to be incorporated into my java program

I still would like an answer to my question regarding the second next instruction of the item that Paul pointed me to

Bob M
 
Ron McLeod
Saloon Keeper
Posts: 2656
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this is late in the game, but I didn't notice your thread until yesterday, and thought I'd ask ...

Have you considered instead of than rolling your own solution working directly with sockets and streaming Java objects, to just use off-the-shelf components to build an HTTP-based mechanism to transport JSON objects between client and server?

There are many proven HTTP server and client implementations to work with - some simple and lightweight, others more complex and capable.

For example - a complete HTTP server working using JSON to exchange Java objects could be as simple as this:
 
Bob Matthews
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron

You have kindly presented code which works as a stand-alone exercise

As socket connection seems pretty straight forward I would prefer to learn how to incorporate your code into my java programs

Bob M
 
Tim Moores
Saloon Keeper
Posts: 5707
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What, specifically, prevents you from incorporating the code? What have you tried? How has that failed? Were there compilation or runtime exceptions?
 
You don't like waffles? Well, do you like this tiny ad?
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!