Method calls inside Controller

38 views
Skip to first unread message

JD Clark

unread,
Jan 26, 2012, 12:31:24 PM1/26/12
to play-framework
In one of my Controllers, I have multiple URLs that will ultimately
render in the same way. For example, this method scans the network on
which the server resides, caches a String representation of each
connected device and each device listening on a specific port, and
then sends that information to another method to render:

public static void networkScan(String networkTarget, String port)
{
//These two lists will never have more than 256 total entries
List<InetSocketAddress> listeningDevices;
Map<String, String> allDevices;

...Logic for discovering network devices...

//Store the results in a cache, for history preservation in the
browser
Cache.set(session.getId() + "listeningDevices", listeningDevices);
Cache.set(session.getId() + "allDevices", allDevices);
showScan(listeningDevices, allDevices);
}

public static void showScan(List<InetSocketAddress> listeningDevices,
Map<String, String> allDevices)
{
render(listeningDevices, allDevices);
}

public static void getCachedScan()
{
List<InetSocketAddress> listeningDevices =
(List<InetSocketAddress>)Cache.get(session.getId() +
"listeningDevices");
Map<String, String> allDevices = (Map<String,
String>)Cache.get(session.getId() + "allDevices");
if(listeningDevices == null)
listeningDevices = new ArrayList<InetSocketAddress>();
if(allDevices == null)
allDevices = new TreeMap<String, String>();

renderScan(listeningDevices, allDevices);
}
Doing it this way results in Play doing some weird array copying that
ends up taking infinite memory. If I were to change my call of
showScan() to simply render() and create a view with the name
networkScan.html, it all works just fine, no memory bugs.

I have several other methods that also use showScan, based on
different caching settings. I don't want lots of views that are all
essentially copies of each other, so I'm trying to go through just one
method with one corresponding view.

Manuel Bernhardt

unread,
Jan 26, 2012, 12:36:09 PM1/26/12
to play-fr...@googlegroups.com
calling a public static method in a controller issues a redirect. If
you don't want the called method to issue a redirect you need to
prefix it with @Util

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>

JD Clark

unread,
Jan 26, 2012, 1:50:29 PM1/26/12
to play-framework
Thank you! I couldn't find information on this very easily when I
looked for it. Is there a way I can help with the documentation of
functionality like this so it's more widely available?

On Jan 26, 12:36 pm, Manuel Bernhardt <bernhardt.man...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages