aspose file tools*
The moose likes Servlets and the fly likes Background classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Background classes" Watch "Background classes" New topic
Author

Background classes

Sean Michael Hayes
Ranch Hand

Joined: Feb 08, 2012
Posts: 54

[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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

What do you mean by "background classes"?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sean Michael Hayes
Ranch Hand

Joined: Feb 08, 2012
Posts: 54

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

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

Joined: Feb 08, 2012
Posts: 54

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

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

    Joined: Feb 08, 2012
    Posts: 54

    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
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61437
        
      67

    Meantime, if you explain what the data is, and where it comes from, some concrete recommendations might be made.
    Piyush Mangal
    Ranch Hand

    Joined: Jan 22, 2007
    Posts: 196
    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

    Joined: Feb 08, 2012
    Posts: 54

    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

    Joined: Feb 08, 2012
    Posts: 54

    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.
    Jayesh A Lalwani
    Bartender

    Joined: Jan 17, 2008
    Posts: 2409
        
      28

    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
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61437
        
      67

    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

    Joined: Feb 08, 2012
    Posts: 54

    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?
    Nauman Hasan
    Ranch Hand

    Joined: Jul 27, 2005
    Posts: 34
    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
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61437
        
      67

    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.
    Pat Farrell
    Rancher

    Joined: Aug 11, 2007
    Posts: 4659
        
        5

    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.

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Background classes