Archive

Posts Tagged ‘java’

How to resolve out of memory errors during maven test phase

November 5, 2012 Leave a comment

Sometimes during maven test phase, jvm out of memory errors occur. It must know that maven tests, are executed in a forked jvm (this for default).
So if you launch maven with the usual parameters -Xmx512M -XX:MaxPermSize=512M etc. etc. these will not propagated to the forked test jvm.
The solution is to use this parameter: -Dmaven.surefire.debug.

So if you want increase the memory used during the test execution, you must use a command like this:
mvn -Dmaven.surefire.debug=”-server -Xmx512M -XX:MaxPermSize=512M” test
here I’ve used the “-server” parameter to force the jvm to use the parallel throughput gc, increasing the performance of the forked jvm about releasing memory.

The maven reference: http://maven.apache.org/plugins/maven-surefire-plugin/examples/debugging.html

Categories: Uncategorized Tags: , , ,

How to send a tx transactional jms message from a local jboss to a remote jboss (version 4.2.3.GA)

September 1, 2010 Leave a comment

To send a jms message from a jboss to a remote jboss destination you have to configure a new JMSResourceAdapter.

This code does not work!

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
    properties.put(Context.PROVIDER_URL, "jnp://192.168.1.131.129:1299");
    InitialContext jndiContext = new InitialContext(properties);

    //[2] Look up connection factory and queue.
    ConnectionFactory connectionFactory = (ConnectionFactory)jndiContext.lookup("UIL2XAConnectionFactory");
    Queue queue = (Queue)jndiContext.lookup("Queue/DataTransferQueue");

This code correctly sends the message but the tx transactional behaviour is not implemented

You have to:
1. deploy a new JMSResourceAdapter duplicating the file hajndi-jms-ds.xml e.g. remote-jms-ds.xml:

<?xml version="1.0" encoding="UTF-8"?>

<connection-factories>
 
  <!-- ==================================================================== -->
  <!-- JMS Stuff                                                            -->
  <!-- ==================================================================== -->

  <!-- The JMS provider loader -->
  <mbean code="org.jboss.jms.jndi.JMSProviderLoader"
	 name="jboss.mq:service=JMSProviderLoader,name=RemoteJMSProvider">
    <attribute name="ProviderName">RemoteJMSProvider</attribute>
    <attribute name="ProviderAdapterClass">
      org.jboss.jms.jndi.JNDIProviderAdapter
    </attribute>
    <!-- The combined connection factory -->
    <attribute name="FactoryRef">XAConnectionFactory</attribute>
    <!-- The queue connection factory -->
    <attribute name="QueueFactoryRef">XAConnectionFactory</attribute>
    <!-- The topic factory -->
    <attribute name="TopicFactoryRef">XAConnectionFactory</attribute>
    <!-- Access JMS via HAJNDI -->
    <attribute name="Properties">
       java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
       java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
       java.naming.provider.url=10.4.0.7:1100
    </attribute>
  </mbean>


  <!-- JMS XA Resource adapter, use this to get transacted JMS in beans -->
  <tx-connection-factory>
    <jndi-name>RemoteJmsXA</jndi-name>
    <xa-transaction/>
    <rar-name>jms-ra.rar</rar-name>
    <connection-definition>org.jboss.resource.adapter.jms.JmsConnectionFactory</connection-definition>
    <config-property name="SessionDefaultType" type="java.lang.String">javax.jms.Topic</config-property>
    <config-property name="JmsProviderAdapterJNDI" type="java.lang.String">java:/RemoteJMSProvider</config-property>
    <max-pool-size>20</max-pool-size>
    <!--security-domain-and-application>JmsXARealm</security-domain-and-application-->
  </tx-connection-factory>
</connection-factories>

2. now you can get the new connection factory in the local jndi (really getting the adapter pointing the remote connection factory) and then get the remote destination via a remote jndi context.

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
    properties.put(Context.PROVIDER_URL, "jnp://10.4.0.7:1100");
    InitialContext localJndiContext = new InitialContext();
    InitialContext remoteJndiContext = new InitialContext(properties);

    //[2] Look up connection factory and queue.
    ConnectionFactory connectionFactory = (ConnectionFactory)localJndiContext.lookup("java:/RemoteJmsXA");
    Queue queue = (Queue)remoteJndiContext.lookup("Queue/DataTransferQueue");
    //now you can send a jms message and the real sending will be done at tx commit time.
     Connection  connection = connectionFactory.createConnection();
     Session session = connection.createSession();
     MessageProducer  sender = session.createProducer(queue);

references:
HowDoIConfigureTheJMSResourceAdapterToUseARemoteConnectionFactory
JBossMQHAOverview

Categories: Uncategorized Tags: , ,

How to sign and verify a message using spring

February 19, 2010 Leave a comment

You can use spring-crypto-utils a wrapper around java’s native cryptography API. I’m very proud of my little collaboration to this project, hosted by Mirko Caserta.

With real little configuration…

    <crypt:keystore id="keystore" location="classpath:keystore.jks" password="password"/>
    <crypt:privateKey id="privateKey" keystore-ref="keystore" alias="test" password="password"/>
    <crypt:signer id="signer" privateKey-ref="privateKey" algorithm="SHA1withRSA"/>

…you can sign a message using a code like this:

public class MyBusinessSigner {

    @Autowired
    private Signer signer;

    public void myBusinessMethod() {
        // I am not a very useful business method but
        // I can do digital signatures!
        byte[] signature = signer.sign(messageToBeTrusted);
    }
}
Categories: Uncategorized Tags: , ,