Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

He's run into it in two pieces of published software; it's nothing he's written. That said, I don't know enough about the output of that tool to say for sure what's wrong; it's the damnedest thing and I can't find anything on Google about it.

Adbot
ADBOT LOVES YOU

ROFLburger
Jan 12, 2006

So im working on a web crawler for a fun hobby type project. I've been reading about how caching DNS lookups could help reduce I/O wait time, but im not really sure how i would go about doing that.

The way the crawler works now is that there are many 'Domain' objects responsible for synchronously crawling pages that belong to a domain, using java.net.HttpURLConnection and java.net.url.openConnection(). Is there a way that i could have each Domain object store the results of the first dns lookup so that it wont have to run a lookup for each subsequent page crawl? I don't know a whole lot about the way DNS lookups work or how the java.net library handles them so any insight would be helpful.

Max Facetime
Apr 18, 2009

ROFLburger posted:

So im working on a web crawler for a fun hobby type project. I've been reading about how caching DNS lookups could help reduce I/O wait time, but im not really sure how i would go about doing that.

The way the crawler works now is that there are many 'Domain' objects responsible for synchronously crawling pages that belong to a domain, using java.net.HttpURLConnection and java.net.url.openConnection(). Is there a way that i could have each Domain object store the results of the first dns lookup so that it wont have to run a lookup for each subsequent page crawl? I don't know a whole lot about the way DNS lookups work or how the java.net library handles them so any insight would be helpful.

This sounds like premature optimization, but the first thing to do would be to see how many DNS lookups are actually happening. You can do this with a network traffic monitoring software like WireShark.

To explain the results it will be helpful to find out what DNS caching settings Java is using (depends on Java version and if running with a security manager) and what is going on in the operating system's DNS cache (e.g. command ipconfig /displaydns on Windows).

Woodsy Owl
Oct 27, 2004
Whats the style consensus on member classes accessing parent class members directly via ParentClass.this versus passing a reference to the member class on construction?

code:
class A {

    String foo = "foo";

    class B {

        void print () {
            System.out.println(A.this.foo);
        }
    }
}
versus

code:
class A {

    String foo = "foo";

    class B {
        
        A parent;

        B (A parent) {
            this.parent = parent;
        }

        void print () {
            System.out.println(parent.foo);
        }
    }
}

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!
The way that's written, you don't have to do either one, just use foo directly.

Volguus
Mar 3, 2009

ROFLburger posted:

So im working on a web crawler for a fun hobby type project. I've been reading about how caching DNS lookups could help reduce I/O wait time, but im not really sure how i would go about doing that.

The way the crawler works now is that there are many 'Domain' objects responsible for synchronously crawling pages that belong to a domain, using java.net.HttpURLConnection and java.net.url.openConnection(). Is there a way that i could have each Domain object store the results of the first dns lookup so that it wont have to run a lookup for each subsequent page crawl? I don't know a whole lot about the way DNS lookups work or how the java.net library handles them so any insight would be helpful.

First google result about java dns caching: http://docs.aws.amazon.com/AWSSdkDocsJava/latest//DeveloperGuide/java-dg-jvm-ttl.html

Woodsy Owl
Oct 27, 2004

Kilson posted:

The way that's written, you don't have to do either one, just use foo directly.

Right, but the question is on style: is it stylistically favorable for member classes to reference parent class members directly or by passing a reference of the parent class to the member class through the member class constructor?

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Woodsy Owl posted:

Right, but the question is on style: is it stylistically favorable for member classes to reference parent class members directly or by passing a reference of the parent class to the member class through the member class constructor?

Why wouldn't direct access to your own containing class be okay? On the other hand, the second style would allow for access to instances of the containing class other than the one that contains your instance, which seems kind of messy to me but might have situational applications.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

Woodsy Owl posted:

Right, but the question is on style: is it stylistically favorable for member classes to reference parent class members directly or by passing a reference of the parent class to the member class through the member class constructor?

The first way, and only when it's not clear whose member you're accessing without it, or when you need to pass the parent class as an argument. I would consider the second approach a big code smell, as it is not only unnecessary (you already have a synthetic accessor to your outer class) but a potential source of both confusion and bugs. If I saw it in the wild, I would assume your inner class had been sloppily transplanted into the outer class without thought.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
A non-static inner class is already pretty tightly coupled to an instance of the outer class - you can't even construct an instance of the inner class without already having an instance of the outer one.

Stylistically, if you want to access members of the containing instance of the outer class, you should just do that. If you pass in an instance of the outer class, that suggests you don't want such a tight coupling (e.g. perhaps the instance of the outer class referred to can be null, or might change over the lifetime of the object, or whatever). If that is the case, it should be a static inner class instead, or just an entirely separate class.

CarrKnight
May 24, 2013
So this is a general question, but I am implementing it in Java so here it is.

I have a somewhat hierarchical structure in my code, something like this:
code:
class Agent{
  
final private Strategy1 strategy;

final private Strategy2 another;

final private int field;
...
Then I have a big constructor that receives all the strategies and fields needed when instantiating the agent:
code:
public Agent(Strategy1 strategy,Strategy2 another, int field)
Now, I need to instantiate this agent from a JSON object
code:
{
  "agent": {
    "strategy1": {
      "type": "nameOfImplementation",
      "constructorParameter1": "...."
    },
    "strategy2": {
      "type": "nameOfImplementation2",
      "constructorParameter1": "...."
    },
    "field": 42
  }
}
What would be the best way to do this? Notice that the various implementations of Strategy1 (and 2) have constructors with different parameters (and potentially sub-strategies).
My current approach is to use Map<String,Factory<Strategy1>> or whatever, but I am not sure that's the best way to go about it.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
It looks a lot like you're trying to solve the Dependency Injection problem. I'd recommend taking a look at some of the mature DI frameworks around and seeing if any of them is suitable for your needs.

M31
Jun 12, 2012
I would use Jackson's polymorphic support (http://wiki.fasterxml.com/JacksonPolymorphicDeserialization) and maybe the @JsonCreator annotation to use the constructor when deserializing.

CarrKnight
May 24, 2013

Jabor posted:

It looks a lot like you're trying to solve the Dependency Injection problem. I'd recommend taking a look at some of the mature DI frameworks around and seeing if any of them is suitable for your needs.

Yes! I think so. I only encountered the definition of DI in testing books but it's correct.

Now my main problem is that I work with a team of academicians and to be honest I can't expect them to go through Guice or Spring. I had a small lecture on what @Nullable and @NonNull mean and if I start adding more weird stuff I make the code illegible for anyone but me.

Is there a framework that is easily readable?
Need to look at Jackson I guess.

Max Facetime
Apr 18, 2009

CarrKnight posted:

What would be the best way to do this? Notice that the various implementations of Strategy1 (and 2) have constructors with different parameters (and potentially sub-strategies).
My current approach is to use Map<String,Factory<Strategy1>> or whatever, but I am not sure that's the best way to go about it.

I would write something like

Java code:
class Agent {
  public static Agent from(JSON json) {
    return new Agent(Strategy1.from(json.get("strategy1")), Strategy2.from(json.get("strategy2")), json.getInt("field"));
  }
  // the rest...
}

interface Strategy1 {
  public static Strategy1 from(JSON json) {
    String type = json.getString("type");
    switch(type) {
      case "nameOfImplementation": return Strategy1Implementation.from(json);
      default: throw new IllegalArgumentException(type);
    }
  }
  // the rest...
}

// etc...
Simple, strongly-typed, performant.

Max Facetime fucked around with this message at 21:05 on May 10, 2015

CarrKnight
May 24, 2013
Awesome. Is there a name for this kind of pattern?


That's pretty much what I do except I make it into a separate factory class, well really Function<Json,Strategy> since I might need to modify the parameters through GUI after they are read but before the strategies are created.

Hughlander
May 11, 2005

CarrKnight posted:

Yes! I think so. I only encountered the definition of DI in testing books but it's correct.

Now my main problem is that I work with a team of academicians and to be honest I can't expect them to go through Guice or Spring. I had a small lecture on what @Nullable and @NonNull mean and if I start adding more weird stuff I make the code illegible for anyone but me.

Is there a framework that is easily readable?
Need to look at Jackson I guess.

If you set up Guice correctly they can just consider it black magic.

Lord Windy
Mar 26, 2010
For Uni, I need to learn Swing. I don't do much GUI work at the best of times, and I've been using FX when I have.

Does anyone have like a small tutorial other than the Oracle one? I'm basically looking for something that shows you how to make a couple of projects ranging from lambda function listeners to something a little more advance. The lectures on it are a bit of a mess.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Cool site I found if anyone wants to host a hobby webapp online or gently caress around with cloud poo poo your managers keep talking about from RedHat. Pretty much the only credible site I found that hosts java projects.

Found it while googling around for a host for a small app a goon asked for. Once you run the set up poo poo it'll generate the SSH token and a git repo for you. When you push your app upstream it automatically compiles and starts the app via Maven.

https://www.openshift.com/

Jo
Jan 24, 2005

:allears:
Soiled Meat

lamentable dustman posted:

Cool site I found if anyone wants to host a hobby webapp online or gently caress around with cloud poo poo your managers keep talking about from RedHat. Pretty much the only credible site I found that hosts java projects.

Found it while googling around for a host for a small app a goon asked for. Once you run the set up poo poo it'll generate the SSH token and a git repo for you. When you push your app upstream it automatically compiles and starts the app via Maven.

https://www.openshift.com/

Don't Heroku and AWS let you host Java applications?

more like dICK
Feb 15, 2010

This is inevitable.
Openshift has a better free plan than heroku, but is worse in every other way.

baquerd
Jul 2, 2007

by FactsAreUseless
Any recommendations for code visualization tools? I'm looking for a tool that can rip through a dozen module project with hundreds of classes using relatively common features such as Spring dependency injection, and produce a graph of all classes, inheritances, and dependencies. Extra points if it includes library dependency support. It should produce graphical output that has easily modifiable visualization, and the modifications should be able to be saved and updated as code changes. For the right tool, tens of thousands of dollars in licensing fees would not be out of the question at all.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

For a software architecture course we used Understand (https://scitools.com/) on decent sized open source projects. I can't comment on the DI support since we didn't really look at that but it seemed to cope with large projects pretty well and had quite a bit of organization/visualization support. It's been a year though.

baquerd
Jul 2, 2007

by FactsAreUseless

carry on then posted:

For a software architecture course we used Understand (https://scitools.com/) on decent sized open source projects. I can't comment on the DI support since we didn't really look at that but it seemed to cope with large projects pretty well and had quite a bit of organization/visualization support. It's been a year though.

This is an interesting tool, thank you. Do you know anything about A-Spice certification as it relates to this? Given the auto background, it seems like a natural choice.

piratepilates
Mar 28, 2004

So I will learn to live with it. Because I can live with it. I can live with it.



more like dICK posted:

Openshift has a better free plan than heroku, but is worse in every other way.

What's so bad about it? I've only briefly messed around with it.

edit: Wait what the 3 gears is 3 gears per account and not per app? Well that's really lovely.

piratepilates fucked around with this message at 06:39 on May 21, 2015

DholmbladRU
May 4, 2006
edit:figured it out

DholmbladRU fucked around with this message at 21:59 on May 26, 2015

Jo
Jan 24, 2005

:allears:
Soiled Meat
If I have a Socket connected from a client to a server and I want to fiddle with a custom protocol, is there a more graceful way of having the server handle commands than either (1) barfing serialized 'ServerCommand' objects across the network with ObjectStream or (2) writing plain strings like, "doActionA"?

Volguus
Mar 3, 2009

Jo posted:

If I have a Socket connected from a client to a server and I want to fiddle with a custom protocol, is there a more graceful way of having the server handle commands than either (1) barfing serialized 'ServerCommand' objects across the network with ObjectStream or (2) writing plain strings like, "doActionA"?

What do you mean by "graceful"? If you're at the socket level, you essentially have to write bytes on the wire. What those bytes mean, that's entirely up to you. There's no way around that. Now, you may come and say that you don't want to serialize objects using ObjectStream. That's fine, there are a billion other ways to serialize objects (to json, to xml, to other binary protocols like protobuf or msgpack).

Or maybe you don't want to send objects at all (or strings). That's fine too, you can send numbers: 1 means actionA, 2 means actionB, etc., but that'll be quite hard to maintain if it gets big, but I suppose you could do it.

But, you can send anything you want. If you can make bytes out of it, you can send the kitchen sink if you so desire.

Jo
Jan 24, 2005

:allears:
Soiled Meat

Volguus posted:

What do you mean by "graceful"? If you're at the socket level, you essentially have to write bytes on the wire. What those bytes mean, that's entirely up to you. There's no way around that. Now, you may come and say that you don't want to serialize objects using ObjectStream. That's fine, there are a billion other ways to serialize objects (to json, to xml, to other binary protocols like protobuf or msgpack).

Or maybe you don't want to send objects at all (or strings). That's fine too, you can send numbers: 1 means actionA, 2 means actionB, etc., but that'll be quite hard to maintain if it gets big, but I suppose you could do it.

But, you can send anything you want. If you can make bytes out of it, you can send the kitchen sink if you so desire.

By graceful, I meant, something less seemingly barbaric than writing a server which reads a character from the socket and throws it into a switch statement, then reads a few more characters and reassembles objects. That feels really old-school, like something I'd do in C in 1990. If that's not actually stupid or horribly unacceptable, I think I'll just make a CommandObject and dump that over the wire. I'd figured Java might have some "community standard" or "de facto" accepted way of doing it. "Oh, actually you're supposed to subclass the CommunicationProtocolWriter," or something like that.

Volguus
Mar 3, 2009

Jo posted:

By graceful, I meant, something less seemingly barbaric than writing a server which reads a character from the socket and throws it into a switch statement, then reads a few more characters and reassembles objects. That feels really old-school, like something I'd do in C in 1990. If that's not actually stupid or horribly unacceptable, I think I'll just make a CommandObject and dump that over the wire. I'd figured Java might have some "community standard" or "de facto" accepted way of doing it. "Oh, actually you're supposed to subclass the CommunicationProtocolWriter," or something like that.

There are already quite a bunch of protocols invented for network communication, but since you said you wanted to create your own there is no really any way around it but send bytes over the wire.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Jo posted:

By graceful, I meant, something less seemingly barbaric than writing a server which reads a character from the socket and throws it into a switch statement, then reads a few more characters and reassembles objects. That feels really old-school, like something I'd do in C in 1990. If that's not actually stupid or horribly unacceptable, I think I'll just make a CommandObject and dump that over the wire. I'd figured Java might have some "community standard" or "de facto" accepted way of doing it. "Oh, actually you're supposed to subclass the CommunicationProtocolWriter," or something like that.

You say barbaric, but it's actually just about efficiency. The network is still usually the bottleneck for performance, so the less you need to send to get the job done, the better (especially if you're writing an application-layer protocol on top of TCP, which is quite chatty on its own.) There's not much point to make sending Java objects part of the command function of the protocol unless you have an application that specifically benefits from doing that in production, but it's possible so go hog wild.

DholmbladRU
May 4, 2006
Having some issues with a java spring POST api.

When I change the RequestBody to a String I can get the string being sent in with the port request. However when it is set to PostCountry I get the below error message. Does anything have thoughts? I am using jackson-core 2.5.3



code:
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
code:
   @RequestMapping(value = "/getCountries"
            , method = RequestMethod.POST
            , consumes = {MediaType.APPLICATION_JSON_VALUE}
            , produces = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseBody
    public String getCountries(@RequestBody PostCountry country) {

        System.out.println(country == null);
        System.out.println(country.country);

 
        return "";

    }
code:
public class PostCountry implements Serializable {


    public String country;

    public PostCountry() {
    }

    public void init() {}

    public void setCountry(String country){
        this.country=country;
    }
    public String getCountry() {
        return country;
    }

}


edit: as a work around I change the ResponseBody to a string. And parsed out that json string using GSON. Seems to be working

DholmbladRU fucked around with this message at 15:54 on Jun 3, 2015

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!
Are you setting the content type on the request to application/json or whatever?

edit: yep, sorry, I missed that it was at the bottom. That's a weird place for it.
Can you verify that the request actually has the correct content-type header?

Kilson fucked around with this message at 20:59 on Jun 3, 2015

DholmbladRU
May 4, 2006

Kilson posted:

Are you setting the content type on the request to application/json or whatever?

Yes you can see in the screenshot the application/type is correctly set to json

DholmbladRU
May 4, 2006
figured it out!

needed this in the mvc-dispatcher file

code:
  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
                <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            </list>
        </property>
    </bean>

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.
I was gonna say, if it works with a string and the string is in the right format then your serialization is messed up somehow, expecting some token it isn't getting or something.

Needing to include jackson could do it :cheeky:

I'm not using Spring atm (although I played around with it once) but it can help with REST APIs to actually return some kind of Response object with the JSON baked into its data field rather than just saying "return string" and counting on the API to map it for you. Plus that way you get more control over the response, if you need to send back an HTML error code or something.

Paul MaudDib
May 3, 2006

TEAM NVIDIA:
FORUM POLICE
No questions, just wanted to share something that popped up on HN: http://java.metagno.me/

DholmbladRU
May 4, 2006
Trying to understand how to approach this problem. I have a java spring application which is running on tomcat. As part of the application I need to build xml(.kml) files which are accessible through a url localhost:port/resources/kml. I am unsure how to write files to the disk or exploded war in order to make them accessible via this url. This seems like a common type of functionality. The kml files will be written for one time use, but they are very large.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

DholmbladRU posted:

Trying to understand how to approach this problem. I have a java spring application which is running on tomcat. As part of the application I need to build xml(.kml) files which are accessible through a url localhost:port/resources/kml. I am unsure how to write files to the disk or exploded war in order to make them accessible via this url. This seems like a common type of functionality. The kml files will be written for one time use, but they are very large.

You don't necessarily need it baked in the WAR file. As long as your spring application is listening to localhost:port/resources/kml and responds with a service, then you can just have your service read the XML files at runtime (so long as they have permission) and then return the file in whatever manner you wish; plaintext HTML or something fancier. Should be pretty straightforward.

Sounds like you need to look into implementing a service to listen to port/resources/kml using spring, and then have that service return your XML.

Adbot
ADBOT LOVES YOU

DholmbladRU
May 4, 2006

Zaphod42 posted:

You don't necessarily need it baked in the WAR file. As long as your spring application is listening to localhost:port/resources/kml and responds with a service, then you can just have your service read the XML files at runtime (so long as they have permission) and then return the file in whatever manner you wish; plaintext HTML or something fancier. Should be pretty straightforward.

Sounds like you need to look into implementing a service to listen to port/resources/kml using spring, and then have that service return your XML.

Accessing the files via url is not the problem. What I am unsure of is how to properly write to that directory, without breaking 'rules'...

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply