<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7372546996843090808</id><updated>2024-09-13T06:44:27.487-07:00</updated><title type='text'>To Turing</title><subtitle type='html'>Discussions on coding and machine learning</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://to-turing.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7372546996843090808/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://to-turing.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ikari</name><uri>http://www.blogger.com/profile/11028618799376886617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7372546996843090808.post-3699790952660082165</id><published>2011-08-08T00:58:00.000-07:00</published><updated>2011-08-08T00:58:56.509-07:00</updated><title type='text'>RabbitMQ Publisher Subscribe Example in Java on Ubuntu</title><content type='html'>To install RabbitMQ on Ubuntu is as simple as executing:&lt;br/&gt;&lt;br&gt;

&lt;i&gt;sudo apt-get install rabbitmq-server&lt;/i&gt;

&lt;br/&gt;&lt;br&gt;

Next we need to download the java client libraries as they are not included in the ubuntu package. You can find the libraries at &lt;a href=&quot;http://www.rabbitmq.com/java-client.html&quot;&gt;http://www.rabbitmq.com/java-client.html&lt;/a&gt; Unzip the archive and include the jars in your classpath
&lt;br&gt;&lt;br&gt;

First we need a way of creating connections to RabbitMQ and also a method to setup exchanges, queues and routing keys:
&lt;textarea rows=&quot;36&quot; cols=65&quot;&gt;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RabbitMQFactory {
    
    private static int DEFAULT_PORT = 5672; // The default port RabbitMQ runs on
    public static final String EXCHANGE = &quot;testExchange&quot;;
    public static final String QUEUE = &quot;testQueue&quot;;
    public static final String ROUTINGKEY = &quot;testRoutingKey&quot;;

    public static Channel getDefaultChannel() throws IOException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setPassword(&quot;guest&quot;);
        factory.setPassword(&quot;guest&quot;);
        factory.setVirtualHost(&quot;/&quot;);
        factory.setHost(&quot;localhost&quot;);
        factory.setPort(DEFAULT_PORT);
        return factory.newConnection().createChannel();
    }

    public static void createQueue(String exchange, String queue, String key) {
        try {
            Channel channel = getDefaultChannel();
            channel.exchangeDeclare(exchange, &quot;direct&quot;, true);
            channel.queueDeclare(queue, true, false, false, null);
            channel.queueBind(queue, exchange, key);
        } catch (IOException ex) {
            Logger.getLogger(RabbitMQFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Create the topics
    public static void main(String[] args) {
        createQueue(EXCHANGE, QUEUE, ROUTINGKEY);
    }

}
&lt;/textarea&gt;
Run the main method to setup our test queue.&lt;br&gt;&lt;br&gt;

Then create the publisher:
&lt;textarea rows=&quot;21&quot; cols=65&quot;&gt;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.Date;
import java.util.Random;

public class Producer {

    public static void main(String[] args) throws IOException, InterruptedException {
        Random random = new Random();
        Channel channel = RabbitMQFactory.getDefaultChannel();
        while(true) {
            String msgText = &quot;Current time is: &quot; + new Date().toString();
            System.out.println(&quot;Adding msg: &quot; + msgText);
            channel.basicPublish(RabbitMQFactory.EXCHANGE, RabbitMQFactory.ROUTINGKEY, null, msgText.getBytes());
            Thread.sleep(random.nextInt(5) * 1000);
        }
    }
}
&lt;/textarea&gt;

&lt;br&gt;&lt;br&gt;
Then the consumer
&lt;textarea rows=&quot;24&quot; cols=65&quot;&gt;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import java.io.IOException;
import java.util.Date;
import java.util.Random;


public class Consumer {

    public static void main(String[] args) throws IOException, InterruptedException {
        Random random = new Random();
        Channel channel = RabbitMQFactory.getDefaultChannel();
        QueueingConsumer cons = new QueueingConsumer(channel);
        channel.basicConsume(RabbitMQFactory.QUEUE, false, cons);
        while(true) {
            Delivery delivery = cons.nextDelivery();
            System.out.println(&quot;Got Message: &quot; + new String(delivery.getBody()));
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    }
}
&lt;/textarea&gt;
&lt;br&gt;&lt;br&gt;
The execute the main method on the Publisher and Consumer and watch the messages flow.</content><link rel='replies' type='application/atom+xml' href='http://to-turing.blogspot.com/feeds/3699790952660082165/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://to-turing.blogspot.com/2011/08/rabbitmq-publisher-subscribe-example-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7372546996843090808/posts/default/3699790952660082165'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7372546996843090808/posts/default/3699790952660082165'/><link rel='alternate' type='text/html' href='http://to-turing.blogspot.com/2011/08/rabbitmq-publisher-subscribe-example-in.html' title='RabbitMQ Publisher Subscribe Example in Java on Ubuntu'/><author><name>Ikari</name><uri>http://www.blogger.com/profile/11028618799376886617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7372546996843090808.post-1966530174023862860</id><published>2011-08-07T04:08:00.000-07:00</published><updated>2011-08-07T04:21:04.891-07:00</updated><title type='text'>Free AI Course From Stanford - Introduction to Artificial Intelligence</title><content type='html'>Standford university&#39;s Sebastian Thrun and Peter Norvig in the next session of CS221: Introduction to Artificial Intelligence (October 2011) are throwing open the doors and allowing anyone, anywhere enrolment into this excellent course. All materials will be available online and there will be a homework assignment each week along with a mid-term and final exam. The best thing is that its free, to as many people who sign up.
&lt;br/&gt;&lt;br/&gt;
Registration is occurring soon so sign up here at &lt;a href=&quot;http://www.ai-class.com/&quot;&gt;www.ai-class.com&lt;/a&gt;.
&lt;br/&gt;&lt;br/&gt;
There is a text book associated with the course.
&lt;iframe src=&quot;http://rcm.amazon.com/e/cm?t=totu00-20&amp;o=1&amp;p=8&amp;l=bpl&amp;asins=0136042597&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;m=amazon&amp;lc1=0000FF&amp;bc1=000000&amp;bg1=FFFFFF&amp;f=ifr&quot; style=&quot;align:left;padding-top:5px;width:131px;height:245px;padding-right:10px;&quot;align=&quot;left&quot; scrolling=&quot;no&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;This text is used in over 1200 universities to teach AI so will be an asset for any bookshelf.</content><link rel='replies' type='application/atom+xml' href='http://to-turing.blogspot.com/feeds/1966530174023862860/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://to-turing.blogspot.com/2011/08/free-ai-course-from-stanford.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7372546996843090808/posts/default/1966530174023862860'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7372546996843090808/posts/default/1966530174023862860'/><link rel='alternate' type='text/html' href='http://to-turing.blogspot.com/2011/08/free-ai-course-from-stanford.html' title='Free AI Course From Stanford - Introduction to Artificial Intelligence'/><author><name>Ikari</name><uri>http://www.blogger.com/profile/11028618799376886617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7372546996843090808.post-2520134740472999606</id><published>2011-07-26T08:24:00.000-07:00</published><updated>2011-07-26T08:24:20.986-07:00</updated><title type='text'>Grails Accessing Raw XML from a Request</title><content type='html'>There have been times in a grails controller I&#39;ve wanted direct access to the xml in a POST request. You can retreive this via the request object: &lt;i&gt;request.reader.text&lt;/i&gt; But what if you need access the raw xml and then to also use the xmlslurper to pick apart the xml. Accessing the &lt;i&gt;request.XML&lt;/i&gt; after &lt;i&gt;request.reader.xml &lt;/i&gt; throws this exception: &lt;i&gt;java.lang.IllegalStateException: getReader() has already been called for this request&lt;/i&gt; &lt;br/&gt;
&lt;br/&gt;
Say I have the following xml:&lt;br/&gt;
&lt;textarea rows=&quot;6&quot; cols=65&quot;&gt;
&lt;one&gt;
  &lt;two&gt;test line&lt;/two&gt;
    &lt;three&gt;
      ...
    &lt;/three&gt;
&lt;/one&gt;
&lt;/textarea&gt;
&lt;br/&gt;
&lt;br/&gt;
and I want to access the raw xml tag &#39;three&#39; but I want programmatic access to the tag &#39;two&#39;. The following allows that:
&lt;textarea rows=&quot;4&quot; cols=65&quot; style=&quot;overflow;&quot;&gt;
def xml = request.reader.text
def three = xml =~ /(?m)(&amp;lt;three.*&amp;lt;\/three&amp;gt;)/ # multine regex
def parsed = new XmlParser().parseText(xml)
def two = parsed.one.two.text()
&lt;/textarea&gt;</content><link rel='replies' type='application/atom+xml' href='http://to-turing.blogspot.com/feeds/2520134740472999606/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://to-turing.blogspot.com/2011/07/grails-accessing-raw-xml-from-request.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7372546996843090808/posts/default/2520134740472999606'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7372546996843090808/posts/default/2520134740472999606'/><link rel='alternate' type='text/html' href='http://to-turing.blogspot.com/2011/07/grails-accessing-raw-xml-from-request.html' title='Grails Accessing Raw XML from a Request'/><author><name>Ikari</name><uri>http://www.blogger.com/profile/11028618799376886617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7372546996843090808.post-4567282037612694244</id><published>2011-07-18T08:03:00.000-07:00</published><updated>2011-07-18T08:03:57.996-07:00</updated><title type='text'>Grails Script to Copy Jar Dependencies</title><content type='html'>The usual way to include jar dependencies in Grails is to set the versioned dependencies in the BuildConfig.groovy. One issue I can see with this is that it forces you to use Maven, Ivy or at the very least a directory structure that is versioned. While this is a good practice and should be encouraged, there are times when you have to work with legacy build systems and you dont have the benefit of good structure. A script such as the following can copy the jars into the lib directory of the grails project. Call it something like Copyjars.groovy and put it in the scripts directory in the root of the grails project.
&lt;br/&gt;
&lt;br/&gt;
&lt;br/&gt;
&lt;textarea rows=&quot;20&quot; cols=65&quot;&gt;
includeTargets &amp;lt;&amp;lt; grailsScript(&quot;Init&quot;)

target(main: &quot;Copy jar dependencies&quot;) {
        Ant.copy(toDir: &quot;${basedir}/lib&quot;, overwrite: true) {
        fileset(file: &quot;${basedir}/../../myproject/lib/*jar&quot;)

  switch (grailsEnv) {
   case &quot;production&quot;:
    fileset(file: &quot;${basedir}/../../myproject/PRD/config-PRD.jar&quot;)
    break
   default:
    fileset(file: &quot;${basedir}/../../myproject/DEV/config-DEV.jar&quot;)
      break
  }
    }
}

setDefaultTarget(main)
 &lt;/textarea&gt;

&lt;br/&gt;
&lt;br/&gt;
&lt;br/&gt;
Then run the script using &quot;grails prod copyjars&quot;</content><link rel='replies' type='application/atom+xml' href='http://to-turing.blogspot.com/feeds/4567282037612694244/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://to-turing.blogspot.com/2011/07/grails-script-to-copy-jar-dependencies.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7372546996843090808/posts/default/4567282037612694244'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7372546996843090808/posts/default/4567282037612694244'/><link rel='alternate' type='text/html' href='http://to-turing.blogspot.com/2011/07/grails-script-to-copy-jar-dependencies.html' title='Grails Script to Copy Jar Dependencies'/><author><name>Ikari</name><uri>http://www.blogger.com/profile/11028618799376886617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7372546996843090808.post-2280302944706925096</id><published>2011-07-15T02:00:00.000-07:00</published><updated>2011-07-15T02:26:54.637-07:00</updated><title type='text'>Grails Jar&#39;d Configuration Property Files</title><content type='html'>Say you have an existing Java project which has a plethora of configuration property files and you want to use some of these properties in your new Grails project. How can you do this? One approach is to:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Put all your property files into a jar and place the jar in the &#39;lib&#39; directory of your grails project&lt;/li&gt;&lt;li&gt;Modify the Config.groovy and add the line - &lt;blockquote&gt;grails.config.locations = [ &quot;classpath:file1.properties&quot;, &quot;classpath:file2.properties&quot; ]&lt;/blockquote&gt;&lt;/li&gt;&lt;li&gt;Then depending where you are in the app you have access to the property variables and can dereference them e.g. ${my.file1.prop}&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;</content><link rel='replies' type='application/atom+xml' href='http://to-turing.blogspot.com/feeds/2280302944706925096/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://to-turing.blogspot.com/2011/07/grails-jard-configuration-property.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7372546996843090808/posts/default/2280302944706925096'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7372546996843090808/posts/default/2280302944706925096'/><link rel='alternate' type='text/html' href='http://to-turing.blogspot.com/2011/07/grails-jard-configuration-property.html' title='Grails Jar&#39;d Configuration Property Files'/><author><name>Ikari</name><uri>http://www.blogger.com/profile/11028618799376886617</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>