Thursday, September 30, 2010

Bulk Data Loading with Hibernate

The most efficient way of loading large amounts of data into Hibernate is using a StatelessSession (provided of course you don't want to keep the objects being loaded in the Persistent Context). The StatelessSession essentially works like a plain JDBC connection with the added bonus of being able to use your mapped persistent classes. You should be able to copy/paste the following into your code:

public void bulkCreate(Collection<?> entities) {
// Open a Stateless Session (this doesn't have a persistence context cache and
// doesn't interact with any other second-level or query cache). Everything
// executed with a Stateless session results in an immediate SQL operation.
StatelessSession session = HibernateUtil.getSessionFactory().openStatelessSession();
session.beginTransaction();
Iterator<?> it = entities.iterator();
while (it.hasNext()) {
session.insert(it.next());
}
session.getTransaction().commit();
session.close();
}

Wednesday, September 29, 2010

Confguring Java mail with JBoss AS 5 and gmail

I've been reading the excellent book: JBoss AS 5 Development and the Example in Chapter 4 creates a Mailer EJB, however the book gives no indication on how to configure Java Mail in JBoss, so I thought I'd share.

  1. Navigate to:

  2. $JBOSS_HOME/server/default/deploy

  3. Open the file: mail-service.xml in your favorite text editor
  4. Edit it as follows:


  5. <server>

    <mbean code="org.jboss.mail.MailService" name="jboss:service=Mail">
    <attribute name="JNDIName">java:/Mail</attribute>
    <attribute name="User">${username}@gmail.com</attribute>
    <attribute name="Password">${password}</attribute>
    <attribute name="Configuration">
    <!-- A test configuration -->
    <configuration>
    <!-- Change to your mail server prototocol -->
    <property name="mail.store.protocol" value="pop3" />
    <property name="mail.transport.protocol" value="smtp" />

    <!-- Change to the user who will receive mail -->
    <property name="mail.user" value="${username}@gmail.com" />

    <!-- Change to the mail server -->
    <property name="mail.pop3.host" value="pop.gmail.com" />

    <!-- Change to the SMTP gateway server -->
    <property name="mail.smtp.host" value="smtp.gmail.com" />
    <property name="mail.smtp.auth" value="true" />
    <property name="mail.smtp.user" value="${username}@gmail.com" />
    <property name="mail.smtp.password" value="${password}" />
    <property name="mail.smtp.ssl.enable" value="true" />
    <property name="mail.smtp.starttls.enable" value="true" />
    <property name="mail.smtp.socketFactory.class"
    value="javax.net.ssl.SSLSocketFactory" />

    <!-- The mail server port -->
    <property name="mail.smtp.port" value="465" />

    <!-- Change the default address mail will be from -->
    <property name="mail.from" value="${username}@gmail.com" />

    <!-- Enable debugging output from the javamail classes -->
    <property name="mail.debug" value="false" />
    </configuration>
    </attribute>
    <depends>jboss:service=Naming</depends>
    </mbean>
    </server>

NOTE: If you are using Google Apps, simply change the gmail.com extension to your-domain.com.

Friday, June 4, 2010

Testing for a number in the ksh

Quick script for testing for a number in the ksh
#!/bin/ksh

if [[ $1 = ?([+-])+([0-9]) ]]; then
echo "true";
else
echo "false";
fi

Wednesday, May 19, 2010

Sending a mail with an attachment

I always end up googling this; so thought I'd add it to my blog - leaving me only one place to look!

To mail a single attachment (with no message):

uuencode filename.txt.gz filename.txt.gz | mailx -s subject email_address

To mail a single attachment with a message:

(cat message.txt; uuencode filename.txt.gz filename.txt.gz) | mailx -s subject email_address

To mail multiple attachments:

(cat message.txt; uuencode filename01.txt.gz filename02.txt.gz && uuencode filename02.txt.gz filename02.txt.gz) | mailx -s subject email_address