Friday, October 14, 2011

Creating a WCF Stand Alone Console App

So I had a problem the other day that really annoyed me. I realized that I couldn't spawn processes directly from IIS. The solution was simple, build a wcf stand alone service that IIS app would call to spawn another process...the result was actually really easy. wcf is almost like remoting, in fact it reminds me a lot of java RMI. It's super ez to setup though...

First you have to provide an interface just like java RMI to serve as a contract between the two processes:

using System.ServiceModel;

[ServiceContract]
    public interface IJSXAdapter
    {
        [OperationContract]
        byte[] GetProcessedImageBytes(string jsxTemplatePath, string[] args);
    }

notice the ServiceContract and the OperationContract attributes

you then of course specify the concrete class (you don't need the attributes in the concrete implementation)


here is the server code:

class Program
        {
            static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(JSXAdapter), new Uri[]{
                        new Uri("http://localhost:8000"),
                        new Uri("net.pipe://localhost")
                    }))
                {
                    host.AddServiceEndpoint(typeof(IJSXAdapter),
                      new BasicHttpBinding(),
                      "Reverse");

                    host.AddServiceEndpoint(typeof(IJSXAdapter),
                      new NetNamedPipeBinding(),
                      "PipeReverse");

                    host.Open();

                    Console.WriteLine("Service is available. " +
                      "Press  to exit.");
                    Console.ReadLine();

                    host.Close();
                }
            }
        }



That's it for the server. In this example I make not one but two ServiceEndpoints or listeners. One http and another net.pipe. In your client you can connect with either..

Now for the client. You specify the same interface (you could potentially share a dll:


[ServiceContract]
    public interface IJSXAdapter
    {
        [OperationContract]
        byte[] GetProcessedImageBytes(string jsxTemplatePath, string[] args);
    }



and the client code (copied from a method)
 XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas();
            readerQuotas.MaxArrayLength = 2147483647;



            BasicHttpBinding httpBinding = new BasicHttpBinding();
            httpBinding.MaxReceivedMessageSize = 2147483647;
            httpBinding.MaxBufferSize = 2147483647;
            httpBinding.ReaderQuotas = readerQuotas;
            ChannelFactory httpFactory = new ChannelFactory(httpBinding,
                                                                                      new EndpointAddress("http://localhost:8000/Reverse"));
            NetNamedPipeBinding pipeBinding = new NetNamedPipeBinding();




            pipeBinding.MaxReceivedMessageSize = 2147483647;
            pipeBinding.MaxBufferSize = 2147483647;            
            pipeBinding.ReaderQuotas = readerQuotas;
            ChannelFactory pipeFactory = new ChannelFactory(pipeBinding,
                                                                                      new EndpointAddress("net.pipe://localhost/PipeReverse"));




            IJSXAdapter httpProxy = httpFactory.CreateChannel();
            IJSXAdapter pipeProxy = pipeFactory.CreateChannel();

            
            
            string[] args = new string[1];
            args[0] = "some string";



            byte[] processedImageBytesFromHttp = httpProxy.GetProcessedImageBytes("some string", args);

            byte[] processedImageBytesFromPipe = pipeProxy.GetProcessedImageBytes("some string", args);
            





Though that looks like a lot most of it is extending the size of how much data can come back. Also keep in mind that this is an example of both end points. in practice you would only connect to one...

Tuesday, August 16, 2011

How to remove jar from grails

How do you prevent a jar from being packaged in a war under grails prod war?

like so...


grails.war.resources = { stagingDir, args ->
delete file: "${stagingDir}/WEB-INF/lib/YOUR-FILE-HERE.jar"
}

Sunday, August 14, 2011

passing JVM arguments to Grails commands (eg run-app)

I ran into a funny situation where I needed to pass some jvm arguments to a grails app. JMX arguments was what I was trying to pass it. I tried a bunch of things but it ended up that I just needed to put them in the GRAILS_OPTS

export GRAILS_OPTS="-Dcom.sun.management.jmxremote"
grails run-app

Saturday, July 30, 2011

mvservice

UPDATE this project has been replaced by https://github.com/asharif/mv-session-mgr

This is finally the cumulation of a couple different projects for mvbase/PICK. It is a stand alone web application (with embedded jetty) that acts as a proxy between the desired mvbase server and the real world ....lol

Currently it provides 2 means of communication RESTful service and RMI

you start the server with java -jar (read the project readme file for command line args https://github.com/asharif/mv-wservice)

for RESTful you look at /mv/sub/{mvsubname}/{barseperatedmvparams} (eg. /mv/sub/hello/A|B)

It also provides JMX for managing the mvconnection beans (configuration is in the readme file)

Embedded Jetty

A lightweight embedded jetty project. Supports JSP 2.0 as well as log4j through slf4j. That's it!

https://github.com/asharif/embdjetty


To use in your project you download repo, compile with

mvn package

this creates a jar file with the good ol' '-with-dependencies'. that file is the server. You need to specify port on java -jar:

-Dport=8080

Now you can drop it in your favorite spring app, compile with the maven assembly plugin and you got a portable web app (well no embedded db but that's another story)

log4j across many dependencies through slf4j

I ran into a stupid problem that made me feel silly. I had a project that depended on a few jars that I myself had made using log4j. Well classloader issues arise if you just add them to classpath and try to run. different loaders load log4j. Hence why it is important to use slf4j and it's binding for log4j. I just added these in the pom and it was all good (scope depends on your project). Also, log4j files go under /src/main/resources in maven....that way they get copied to WEB-INF/classes/



org.slf4j
slf4j-api
1.6.1
runtime


org.slf4j
slf4j-log4j12
1.6.1
runtime


log4j
log4j
1.2.16
runtime

Tuesday, June 28, 2011

New github repo for mv.jdbc

UPDATE: This repo is obsolete it's replaced by https://github.com/asharif/mv-session-mgr

Created a new project to attempt to be connecting to mvBase in a uniform JDBC compliant way. It also handles multiple threads as the original MVSP connectors don't somehow...(yikes!)

Check it out:

https://github.com/asharif/mv.jdbc