Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Background classes

 
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[EDIT: split off from unrelated question. Please start a new post for new questions.]

Just one more quick question, if I have java classes that collect and update variables in the background as it runs and I turn on the server, do the classes work in the background or do I have to run the class as well? I am using eclipse.
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "background classes"?
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:What do you mean by "background classes"?



Well, before I added jsp to my project, it was just a normal java project. Lets say I had a counter that incremented every second in real time in my java class and my jsp is basic and just retrieves the current value when it starts. When I start the server and my jsp file reads the current value of the counter it would come up as zero wouldn't it? Would my counter still be incrementing every second in the background even if the jsp only shows zero?

Apologies if I don't make much sense, I suck at explaining things.
 
Bear Bibeault
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's causing these classes to "run in the background"? We can't help you if you don't tell us what your code is doing.
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:What's causing these classes to "run in the background"? We can't help you if you don't tell us what your code is doing.



Right to be specific, my java classes read data off an xml file and create objects off of the data. These objects are then stored in an arrayList. What I want to do is list off these objects on a webpage. My jsp is pretty basic in that it reads the size of the object arrayList and creates tables for them.

However, no tables are created when I turn on the server and access it through my localhost. I reasoned that this may be because when the java program is running the arrayList has nothing in it initially and is populated as it runs and that the jsp only takes the initial value (zero) and thus creates NO tables at all. But what I want to know is....is the program running alongside the server and would it have populated arraylist?

 
Bear Bibeault
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing runs at all unless it is made to. Perhaps you should read the Servlet Spec to understand how web apps operate, which is much different than standalone programs.

But the whole concept of "running in the background" is unnatural for web apps.

If your JSP needs a data construct to display, its controller should fetch that info from the DB or wherever it gets the data from and feed it to the page.

If you are a newcomer to web apps, I strongly suggest reading these articles:
  • The Secret Life of JSPs
  • The Front Man

  • The first explains what JSP is all about, the second how web apps should be constructed.
     
    Sean Michael Hayes
    Ranch Hand
    Posts: 54
    Android
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:Nothing runs at all unless it is made to. Perhaps you should read the Servlet Spec too understand how web apps operate, which is much different than standalone programs.

    But the whole concept of "running in the background" is unnatural for web apps.

    If your JSP needs a data construct to display, its controller should fetch that info from the DB or wherever it gets the data from and feed it to the page.

    If you are a newcomer to web apps, I strongly suggest reading these articles:

  • The Secret Life of JSPs
  • The Front Man

  • The first explains what JSP is all about, the second how web apps should be constructed.



    Fair enough, I'll do just that. Thank you.
     
    Bear Bibeault
    Sheriff
    Posts: 67595
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Meantime, if you explain what the data is, and where it comes from, some concrete recommendations might be made.
     
    Ranch Hand
    Posts: 196
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    SeanMichael Hayes wrote:

    Bear Bibeault wrote:What's causing these classes to "run in the background"? We can't help you if you don't tell us what your code is doing.



    Right to be specific, my java classes read data off an xml file and create objects off of the data. These objects are then stored in an arrayList. What I want to do is list off these objects on a webpage. My jsp is pretty basic in that it reads the size of the object arrayList and creates tables for them.

    However, no tables are created when I turn on the server and access it through my localhost. I reasoned that this may be because when the java program is running the arrayList has nothing in it initially and is populated as it runs and that the jsp only takes the initial value (zero) and thus creates NO tables at all. But what I want to know is....is the program running alongside the server and would it have populated arraylist?



    Your background process and Web server will run in different JVMs so web application will not be able to see the data from background process. If you want to reuse the java classes , then deploy your class as a part of web app and have a ServletContextListener bootstraping this class.
     
    Sean Michael Hayes
    Ranch Hand
    Posts: 54
    Android
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:Meantime, if you explain what the data is, and where it comes from, some concrete recommendations might be made.



    In its current form, my java project polls OIDs from several machines using SNMP. The OIDs polled for each machine is added into an Arraylist<String> in that machine's java object constructor. We then loop through that Arraylist and use a "get" method to poll the value of that OID. The value of which is appended to a string called "DeviceReading" with a ',' seperating the values . We then print it out into .csv file, the ',''s seperating the values into different columns in the file.

    What I want to do is add a web tier, in which the values for each machine are printed out onto a web page hosted on a server. Obviously, I'm going into this kinda blind so any pointer would be appreciated.
     
    Sean Michael Hayes
    Ranch Hand
    Posts: 54
    Android
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piyush Mangal wrote:

    SeanMichael Hayes wrote:

    Bear Bibeault wrote:What's causing these classes to "run in the background"? We can't help you if you don't tell us what your code is doing.



    Right to be specific, my java classes read data off an xml file and create objects off of the data. These objects are then stored in an arrayList. What I want to do is list off these objects on a webpage. My jsp is pretty basic in that it reads the size of the object arrayList and creates tables for them.

    However, no tables are created when I turn on the server and access it through my localhost. I reasoned that this may be because when the java program is running the arrayList has nothing in it initially and is populated as it runs and that the jsp only takes the initial value (zero) and thus creates NO tables at all. But what I want to know is....is the program running alongside the server and would it have populated arraylist?



    Your background process and Web server will run in different JVMs so web application will not be able to see the data from background process. If you want to reuse the java classes , then deploy your class as a part of web app and have a ServletContextListener bootstraping this class.



    Do you have any examples? All the tutorials I can find only cover the basics.
     
    Rancher
    Posts: 2759
    32
    Eclipse IDE Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You won't get an example that will help you exactly. You will need to do the work of breaking down the problem into sub-problems, and then work on solving the sub-problems. You will get plenty of examples if you are looking at solving small problems.
     
    Bear Bibeault
    Sheriff
    Posts: 67595
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I don't agree with that approach (separate threads in the web app).

    I've done similar things before and I would keep the asynchronous polling in a separate Java daemon that writes the data into a database. The web app is simply a view onto that data and can fetch it from the DB whenever a user requests it from a browser.
     
    Sean Michael Hayes
    Ranch Hand
    Posts: 54
    Android
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:I don't agree with that approach (separate threads in the web app).

    I've done similar things before and I would keep the asynchronous polling in a separate Java daemon that writes the data into a database. The web app is simply a view onto that data and can fetch it from the DB whenever a user requests it from a browser.



    So what your saying is that a database (like mySql?) is the way to go?
     
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    One simple approach would be run your program and then output the file you generated to a webserver (like Apache). Then it will be available to view via you internal website.

    ~Nauman
     
    Bear Bibeault
    Sheriff
    Posts: 67595
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    SeanMichael Hayes wrote:So what your saying is that a database (like mySql?) is the way to go?


    While there may be other ways to share the info (files, for example) a DB is a customary way to handle this and takes care of concurrency issues that files create.
     
    Rancher
    Posts: 4686
    7
    Mac OS X VI Editor Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    SeanMichael Hayes wrote:In its current form, my java project polls OIDs from several machines using SNMP. The OIDs polled for each machine is added into an Arraylist<String> in that machine's java object constructor. We then loop through that Arraylist and use a "get" method to poll the value of that OID. The value of which is appended to a string called "DeviceReading" with a ',' seperating the values . We then print it out into .csv file, the ',''s seperating the values into different columns in the file.

    What I want to do is add a web tier, in which the values for each machine are printed out onto a web page hosted on a server. Obviously, I'm going into this kinda blind so any pointer would be appreciated.



    So your "web tier" is simply a view into the current status? Or a "Dashboard" as the folks selling overpriced middleware used to say?

    The standard term for a background task is a daemon. In Java, they are written as Thread objects which are run in parallel. There are a large number of APIs in the java.util.concurrent package that help with this. One of the regulars here, Henry Wong, has written a very popular book on Java Threads.

    Depending on how often the values change, storing the data in a DB may be a bad idea. That said, a design that treats the data as if it were in a data store, and the Controller simply retrieves values from the data store for display is a good thing. How you implement the data store becomes an implementation detail. Good design should work with many implementations, including a DB such as MySql, or a special Daemon thread.

    That said, writing correct multi-threaded Java code is non-trivial. There are lots of ways to screw it up, and most web-programmers don't ever deal with the issues, features and
    bugs of such a system.

     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic