• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I explained the 'static' error above

Then Paul referred me to a FAQ page which I read and tried to implement
But came stuck on the second next instruction (as I explained above)

Bob M
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't get too hung up on that tutorial to make progress on the main issue. You can learn about static fields on https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Bottom line: instance fields can't be used from static methods (which is what main is). An easy solution for this pair of classes would be to make the fields static. While that would be OK to make this example work, it would be the wrong solution once you integrate the code into the overall codebase. But the problem wouldn't manifest there, because you wouldn't have static methods.
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim

I must point out that I have a very narrow and limited knowledge of the java language

I only understand a very small portion of its workings

I understand that I need to make some changes to the code which you posted and which works as a stand-alone example, before I can incorporate it into my java program

I think that I need to remove the word 'static' and the word 'main' but that's it

So am floundering due to lack of knowledge

Bob M
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some progress to date..................................

Added the word 'static to both classes (Socket Server and Socket Client) code

Clean compile on Socket Server code but

1 compile error on the Socket Client code

reference to Trading_Decision_2 (original line 68)

error] cannot make a static reference to the non-static field Trading_Decision_2

which is a variable calculated earlier on in the large java program

what changes do I need to make to the variable Trading_Decision_2 ?

Bob M
New Zealand
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seeing as you have put a lot of effort into this, I'll make an exception to our rule and post the full code of client and server that use no static fields. There's a static main method, but that's just for testing.

In the server code, the important lines are 9 and 10, which you need to incorporate into your larger code. On the client side, it's lines 16 and 17.

The code completely punts on error handling, which obviously your code can't do once you put it into use.

The other issue is that the server waits until "exit" is sent before returning. So you can't call it from the main thread of your larger app, as it would block it. It needs to be called from a background thread.




 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On Client side is it lines 14 & 15 ?

Bob M
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh right, I had edited the post.
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim

I have removed lines 9-16 of the Socket Client code above

In my larger program I have the following.............



but I feel there is no message being sent ?

Bob M
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean, "you feel"? Is anything output on the client or the server side? Any exceptions?
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No

Server side: background thread starts and server listens

Client side: I don't see "we get to here - zz"

Bob M
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim

On the client side:
error: SocketClient exception occurred: java.net.ConnectException: Connection refused: connect

On the server side:


I successfully start a NewThread

but how do I setup the socket connection to run on THAT new thread ?

Bob M
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim

I have replaced an ethernet cord on Computer 1

I am getting no errors on Computer 2 (client)

But on Computer 1 (server) the error is

java.lang.NullPointerException

Bob M
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In which line of code does that happen? Post the full stack trace.
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

what changes in the code are required to print out a 'full stack trace' ?
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding "e.printStackTrace();" to the catch block around the failing instruction.
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim

Here is the full Stack Trace

Time               Messages                                                                                      
2019-06-18 23:00:03 at java.lang.Thread.run(Unknown Source)                                                      
2019-06-18 23:00:03 at com.dukascopy.api.impl.execution.f$a.run(L:926)                                            
2019-06-18 23:00:03 at com.dukascopy.api.impl.execution.f$a.e(L:904)                                              
2019-06-18 23:00:03 at java.util.concurrent.FutureTask.run(Unknown Source)                                        
2019-06-18 23:00:03 at com.dukascopy.api.impl.execution.m.call(L:23)                                              
2019-06-18 23:00:03 at com.dukascopy.api.impl.execution.m.call(L:63)                                              
2019-06-18 23:00:03 at com.dukascopy.api.impl.execution.m.cu(L:85)                                                
2019-06-18 23:00:03 at my_strategies.us_copiosus_GBPAUD_1010.onBar(us_copiosus_GBPAUD_1010.java:471)              
2019-06-18 23:00:03 at my_strategies.us_copiosus_GBPAUD_1010$SocketServer.start(us_copiosus_GBPAUD_1010.java:1720)
2019-06-18 23:00:03 java.lang.NullPointerException                                                                
2019-06-18 23:00:03 SocketServer exception occurred: java.lang.NullPointerException                              
2019-06-18 22:08:30 Strategy Started: GBP_AUD                                        

Line 1720 = Line 9 above (server side)  

server.start();
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So "server" is null? Maybe there's a restriction on opening ports on that machine?
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the server side:


I believe the above code is just not quite right
 
Marshal
Posts: 4499
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a data point for your troubleshooting .. when I run your code (ignoring the the bits for NewThread, and replacing myConsole.getOut() with System.out), I don't get an exception.

 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ron

Having spawned a new background thread, how does one ensure that you begin to open a server socket connection on THAT thread rather than on the main thread ?
 
Ron McLeod
Marshal
Posts: 4499
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is NewThread?  Does it have something to do with launching an instance of SocketServer?
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is followed by code to initiate a socket server connection - see above

But is it related ? - I don't know - thus my question above
 
Ron McLeod
Marshal
Posts: 4499
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It NewThread a class that you created?  Can you share the code?
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ron McLeod
Marshal
Posts: 4499
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want instantiate and run SocketServer in the new thread that code should be inside the thread's run method.

Something like this (not finished code):
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ron

clear answer to my question

current code is now :........................


and stack trace is now...................

Time               Messages                                                                                      
2019-06-20 02:00:02 at my_strategies.us_copiosus_GBPAUD_1010$NewThread.run(us_copiosus_GBPAUD_1010.java:1726)    
2019-06-20 02:00:02 at my_strategies.us_copiosus_GBPAUD_1010$SocketServer.start(us_copiosus_GBPAUD_1010.java:1710)
2019-06-20 02:00:02 java.lang.NullPointerException                                                                
2019-06-20 02:00:02 SocketServer exception occurred: java.lang.NullPointerException

Line 1726 of my main java program = server.start(); line 50 - see above                        
 
Ron McLeod
Marshal
Posts: 4499
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having all your classes stuffed in to a single 2000 line uber source file makes troubleshooting difficult, especially those trying to help only have visibility to bits and pieces.

I ran the last code that you have posted, it works as expected (I've only changed writing debugging messages to System.out).
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks to Tim and Ron (esp. for your patience)

I was mistakenly sending a null message - hence the error

I now have everything working

Sockets and thread are all new territory for me

A final question:

Suppose I add another two clients to the mix

The server starts its background thread very early on in the main java program
The clients start their background threads after a lot of calculations
Thus the server is listening before the clients are sending their messages

Is there a simple way to code such that the server waits until it has received all 3 messages from the three clients (one message each)
before it continues to make its FINAL overall trading decision based on all 4 individual trading decisions (3 clients and itself) ?

Hopefully, without involving any new concepts such as schedulers

Bob M
Dunedin
New Zealand

 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ron

This is my latest code....................................

[A] Server launcher


[B] Server code


[C] Client launcher


[D] Client code


I get a message on the server saying - New thread is running, but that's about all
No error messages I can see

Am I still missing some code?

Bob M
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic