Pages

Friday 29 August 2014

Junit tips and tricks

Since I don't know how your factory method looks like, all I can advise right now is to
  1. Check to see the object is the correct concrete implementation you were looking for:
    IMyInterface fromFactory = factory.create(...);  
    Assert.assertTrue(fromFactory instanceof MyInterfaceImpl1);
  2. You can check if the factory setup the concrete instances with valid instance variables.

Wednesday 27 August 2014

Creating empty directories using jdeb plugin maven

<!-- Package data (templates [empty directories]) -->
        <data>
         <type>template</type>
         <paths>
          <path>/var/lib/direct/direct-cert-discovery-tool</path>
          <path>/var/lib/direct/mail/badmsg</path>
          <path>/var/lib/direct/mail/inmsg</path>
          <path>/var/lib/direct/mail/outmsg</path>
          <path>/var/lib/direct/mail/rawmsg</path>
         </paths>
        </data>

Monday 18 August 2014

Sunday 17 August 2014

Resetting rabbitmq queues

rabbitmq-server -detached #start the server
rabbitmqctl status #check the broker status











sudo rabbitmqctl list_queues
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app











Commands:
    stop [<pid_file>]
    stop_app
    start_app
    wait <pid_file>
    reset
    force_reset
    rotate_logs <suffix>
    cluster <clusternode> ...
    force_cluster <clusternode> ...
    cluster_status
    add_user <username> <password>
    delete_user <username>
    change_password <username> <newpassword>
    clear_password <username>
    set_user_tags <username> <tag> ...
    list_users
    add_vhost <vhostpath>
    delete_vhost <vhostpath>
    list_vhosts [<vhostinfoitem> ...]
    set_permissions [-p <vhostpath>] <user> <conf> <write> <read>
    clear_permissions [-p <vhostpath>] <username>
    list_permissions [-p <vhostpath>]
    list_user_permissions [-p <vhostpath>] <username>
    list_queues [-p <vhostpath>] [<queueinfoitem> ...]
    list_exchanges [-p <vhostpath>] [<exchangeinfoitem> ...]
    list_bindings [-p <vhostpath>] [<bindinginfoitem> ...]
    list_connections [<connectioninfoitem> ...]
    list_channels [<channelinfoitem> ...]
    list_consumers [-p <vhostpath>]
    status
    environment
    report
    eval <expr>
    close_connection <connectionpid> <explanation>
    trace_on [-p <vhost>]
    trace_off [-p <vhost>]
    set_vm_memory_high_watermark <fraction>

Thursday 14 August 2014

How to get the full redirect url in curl ?

curl’s -w option and the sub variable url_effective is what you are looking for.
Something like
curl -Ls -o /dev/null -w %{url_effective} http://google.com
More info
-L         Follow redirects
-s         Silent mode. Don't output anything
-o FILE    Write output to <file> instead of stdout
-w FORMAT  What to output after completion

Tuesday 12 August 2014

What is the web.xml file used for ?

Generally speaking, this is the configuration file of web applications in java. It instructs the servlet container (tomcat for ex.) which classes to load, what parameters to set in the context, and how to intercept requests coming from browsers.
There you specify:
  • what servlets (and filters) you want to use and what URLs you want to map them to
  • listeners - classes that are notified when some events happen (context starts, session created, etc)
  • configuration parameters (context-params)
  • error pages, welcome files
  • security constriants
In servlet 3.0 many of the web.xml parts are optional. These configurations can be done via annotations (@WebServlet@WebListener)

Monday 11 August 2014

Maven assembly plugin

The Maven Assembly plugin is a plugin you can use to create arbitrary distributions for your applications. You can use the Maven Assembly plugin to assemble the output of your project in any format you desire by defining a custom assembly descriptor.
we’re going to use the predefined jar-with-dependencies format. To configure the Maven Assembly Plugin, we need to add the following plugin configuration to our existing build configuration in the pom.xml.


<build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>

Once you’ve added this configuration, you can build the assembly by running the assembly:assembly goal. In the following screen listing, the assembly:assembly goal is executed after the Maven build reaches the install lifecycle phase:
 While it is certainly valid to execute a plugin goal directly from the command-line as we just demonstrated, it is more consistent with the design of Maven to configure the Assembly plugin to execute the assembly:assembly goal during a phase in the Maven lifecycle.

The following plugin configuration configures the Maven Assembly plugin to execute the attached goal during the package phase of the Maven default build lifecycle. The attached goal does the same thing as the assembly goal. To bind to assembly:attached goal to thepackage phase we use the executions element under plugin in the build section of the project’s POM.

<plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>simple-command</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
Once you have this configuration in your POM, all you need to do to generate the assembly is run mvn package. The execution configuration will make sure that the assembly:attached goal is executed when the Maven lifecycle transitions to the package phase of the lifecycle. The assembly will also be created if you run mvn install as the package phase precedes the install phase in the default Maven lifecycle.

Sunday 10 August 2014

Running without using jars in the classpath


The Exec plugin makes it possible for us to run the simplest weather program without having to load the appropriate dependencies into the classpath. In any other build system, we would have to copy all of the program dependencies into some sort of lib/ directory containing a collection of JAR files. Then, we would have to write a simple script that includes our program’s bytecode and all of our dependencies in a classpath. Only then could we run java org.sonatype.mavenbook.weather.Main. The Exec plugin leverages the fact that Maven already knows how to create and manage your classpath and dependencies.


mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main \
      -Dexec.args="70112"


This is convenient, but it’s also nice to know exactly what is being included in your project’s classpath. Although the project depends on a few libraries such as Dom4J, Log4J, Jaxen, and Velocity, it also relies on a few transitive dependencies. If you need to find out what is on the classpath, you can use the Maven Dependency plugin to print out a

mvn dependency:resolve
  

mvn dependency:tree

Sunday 3 August 2014

Consuming REST Services using Spring


  1.  RestTemplatemakes interacting with most RESTful services a one-line incantation. And it can even bind that data to custom domain types.
  2. As you can see, this is a simple Java class with a handful of properties and matching getter methods. It’s annotated with @JsonIgnoreProperties from the Jackson JSON processing library to indicate that any properties not bound in this type should be ignored.
  3. Because the Jackson JSON processing library is in the classpath, RestTemplate will use it (via a message converter) to convert the incoming JSON data into a Page object. From there, the contents of the Page object will be printed to the console.
    Here you’ve only used RestTemplate to make an HTTP GET request. But RestTemplatealso supports other HTTP verbs such as POSTPUT, and DELETE.
  4. http://spring.io/guides/gs/consuming-rest/

What does the spring framework do ?


  1. Dependency Injection
  2. AOP (Aspect oriented programming)
  3. Boiler plate coding removal