Categories
discuss

How do I efficiently cache objects in Java using available RAM?

I need to cache objects in Java using a proportion of whatever RAM is available. I’m aware that others have asked this question, but none of the responses meet my requirements.

My requirements are:

  • Simple and lightweight
  • Not dramatically slower than a plain HashMap
  • Use LRU, or some deletion policy that approximates LRU

I tried LinkedHashMap, however it requires you to specify a maximum number of elements, and I don’t know how many elements it will take to fill up available RAM (their sizes will vary significantly).

My current approach is to use Google Collection’s MapMaker as follows:

Map<String, Object> cache = new MapMaker().softKeys().makeMap();

This seemed attractive as it should automatically delete elements when it needs more RAM, however there is a serious problem: its behavior is to fill up all available RAM, at which point the GC begins to thrash and the whole app’s performance deteriorates dramatically.

I’ve heard of stuff like EHCache, but it seems quite heavy-weight for what I need, and I’m not sure if it is fast enough for my application (remembering that the solution can’t be dramatically slower than a HashMap).

Answer

I’ve got similar requirements to you – concurrency (on 2 hexacore CPUs) and LRU or similar – and also tried Guava MapMaker. I found softValues() much slower than weakValues(), but both made my app excruciatingly slow when memory filled up.

I tried WeakHashMap and it was less problematic, oddly even faster than using LinkedHashMap as an LRU cache via its removeEldestEntry() method.

But by the fastest for me is ConcurrentLinkedHashMap which has made my app 3-4 (!!) times faster than any other cache I tried. Joy, after days of frustration! It’s apparently been incorporated into Guava’s MapMaker, but the LRU feature isn’t in Guava r07 at any rate. Hope it works for you.

Categories
discuss

Reusing a template page in Spring MVC

What is the best and easiest technology from the followings?

Tiles, velocity or freemaker?

Thank you.

Answer

There’s no “best”, but it’s good to know that JSP as being a view technology already provides the <jsp:include> tag for this. E.g.

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2158749</title>
    </head>
    <body>
        <jsp:include page="menu.jsp" />
        <h1>Content</h1>
    </body>
</html>

where in you can just code menu.jsp as if it’s a part of the parent page:

<ul>
    <li><a href="home">Home</a></li>
    <li><a href="faq">FAQ</a></li>
    <li><a href="content">Content</a></li>
</ul>

There are two “standard” alternatives: the @include directive and the JSTL <c:import> tag.

The difference is that the @include directive includes the page during compile time (thus it will happen only once), while the <jsp:include> includes the page during runtime (which has actually the benefit that you can include another dynamic content).

Further is the difference of <c:import> that it includes the generated output of the page and thus not the source code as both <jsp:include> and @include does. The major benefit of <c:import> is however that you can include external resources this way. E.g.

<c:import url="http://google.com" />
Categories
discuss

Has PastryKit been successfully implemented by anyone other than Apple?

Has PastryKit been successfully implemented by anyone other than Apple?

This article and others give a detailed breakdown and source code of PastryKit but has anyone actually got their own content into this framework?

http://davidbcalhoun.com/2009/pastrykit-digging-into-an-apple-pie

Answer

They never officially released it so I doubt you can simply grab it and use it in your own project. Assume it is (C) Apple.

Categories
discuss

Use Maven to trigger a wsgen & wsimport in a row, using wsdlLocation

I have hard times using maven to generate my web service client. So please refer to Creating a web-service client directly from the source for the first part of my question.

To keep it simple and short, I want to go from here (a file in src/main/java) :

package com.example.maven.jaxws.helloservice;
   
import javax.jws.WebService;

@WebService
public class Hello {
     public String sayHello(String param) {
         return "Hello " + param;
     }
} 

to there :

/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.7-b01-
 * Generated source version: 2.1
 * 
 */
@WebServiceClient(name = "HelloService", targetNamespace = "http://helloservice.jaxws.maven.example.com/", wsdlLocation = "http://localhost:8080/test/")
public class HelloService extends Service {

    private final static URL HELLOSERVICE_WSDL_LOCATION;
    private final static Logger logger = 
         Logger.getLogger(com.example.wsimport.HelloService.class.getName());
    ...etc

using only 1 pom.xml file.

Please note the wsdlLocation set on the end. The pom.xml file will probably use both maven-jaxws-plugin wsgen AND wsimport with some tricky configuration to achieve this.

Answer

Assuming, you’re not going to try to use the generated stubs in the same project that you’re doing this in (which would create circular dependencies and be a bad idea…) then, yes, you can do something like this.

The config is not all that tricky, in fact you’d kind of guessed it in your question but here goes:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>generate-wsdl</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>wsgen</goal>
                    </goals>
                    <configuration>
                        <sei><!-- fully qualified class name goes here --></sei>
                        <genWsdl>true</genWsdl>
                    </configuration>
                </execution>
                <execution>
                    <id>generate-stubs</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                    <configuration>
                        <wsdlDirectory>target/jaxws/wsgen/wsdl</wsdlDirectory>
                        <wsdlFiles>                   
                            <wsdlFile><!-- class name goes here -->Service.wsdl</wsdlFile>
                        </wsdlFiles>
                        <!-- *** you need the next line to set the wsdlLocation in the generated stubs *** -->
                        <wsdlLocation>http://localhost:8080/test</wsdlLocation> 
                    </configuration>
                </execution>
            </executions>
        </plugin>

Update – to package up the generated code into a jar I would use the maven-jar-plugin like so:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <id>package-wsclient-jars</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classesDirectory>target/jaxws/<!-- rest of the path here, can't remember it right now --></classesDirectory>
                            <classifier>wsclient</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

I’ve quickly pasted this from our config but our usage is a little different (as we zip up the wsdl files not the generated client but I believe this will get you pretty close). You’ll probably have to setup the version for the maven-jar-plugin if you’re not already using it – 2.3.1 seems to be the latest.

Categories
discuss

Enable/disable asp.net validator controls within a specific “ValidationGroup” with jQuery?

I know how to enable/disable individual validator controls on the client side using

ValidatorEnable(validator, false);

But how do you enable/disable all the validators within a ValidationGroup?

Answer

The validator properties aren’t rendered as attributes unfortunately, so I don’t know a good way to select them directly. You can try to iterate the Page_Validators array and filter out the ones you want to work with.

Try:

$.each(Page_Validators, function (index, validator){
   if (validator.validationGroup == "your group here"){

      ValidatorEnable(validator, false);

   }
});
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..