how to get java to capture printing from sybase stored proc

In my experience on Wall St, Sybase store proc can get very complex. A basic technique is the lowly “print”. It beats “select” because under error condition all selects into a log table are rolled back.

Sometimes Sybase print output doesn’t get returned to java. For jdbc I had a simple reusable method to while-loop through a series of warnings. Here’s my technique for spring jdbcTempalte. Note the documented “logging all warnings” may not work. If you don’t override handleWarnings() like I did, then all warnings become exceptions so super.query() return value is lost — real show stopper.

public class GenericProcedureCaller extends JdbcTemplate {
@Override
protected void handleWarnings(Statement stmt) {
try {
super.handleWarnings(stmt);
} catch (SQLWarningException e) {
log.info("\t\t v v v output from database server v v v v ");
SQLWarning warn = e.SQLWarning();
while (warn != null) {
log.info(warn);
warn = warn.getNextWarning();
}
log.info("\t\t ^ ^ ^ output from database server ^ ^ ^ ^ ");
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public List query(String sql, RowMapper rowMapper) throws DataAccessException {
boolean oldSetting = isIgnoreWarnings();
// setting to false to capture "prints" from the proc, but there's side effect.
setIgnoreWarnings(false);
try {
return super.query(sql, rowMapper);
} finally {
setIgnoreWarnings(oldSetting);
}
}

Advertisements

factory-method vs FactoryBean.java in Spring

I feel there’s deeper and richer support for FactoryBean.java. Notes from spring doc —

— “factory bean” vs FactoryBean —
When the Spring documentation makes mention of a ‘factory bean’, this will be a reference to a bean that is configured in the Spring container that will create objects via an instance or static factory method. When the documentation mentions a FactoryBean (notice the capitalization) this is a reference to a Spring-specific FactoryBean.

— getting the FactoryBean object itself —
Finally, there is sometimes a need to ask a container for an actual FactoryBean instance itself, not the bean it produces. This may be achieved by prepending the bean id with ‘&’ when calling the getBean method of the BeanFactory (such as the ApplicationContext bean factory).

So for a given FactoryBean with an id of myBean, invoking getBean(“myBean”) on the container will return the product of the FactoryBean, but invoking getBean(“&myBean”) will return the FactoryBean instance itself.

FactoryBean to avoid hardcoded classnames in spring xml

Without this, you need to hard code the class name in a windows-specific xml, and a different classname in the linux-specific xml

public class CalcLibFactoryBean implements FactoryBean {
@Override
public CalcLibWrapper getObject() throws Exception {
if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
return new PxYldCalcWrapperStub();
}else {
return new PxYldCalcWrapper();
}
}

convenient spring jdbc methods

1a) Query for map – if your query/proc returns just a single row and you
don’t want to create a new bean class.
* Column names had better be distinct, which is usually easy.
* Similar to boost tuple.
2) Query for object (of your custom class) – if your proc/query returns
a single row, and you already have a bean class to hold all attributes.
3) queryForObject (String sql, String.class) – if you are sure to get
only one row and combine all columns into a single string
4a) Query for int – if your query/proc returns nothing but an integer
4b) Query for object – if your proc/query returns a single Date
5) Row mapper – if your query/proc returns a result set

simplest spring-jmx setup to configure a trading server

Without any annotation or source code change, any public (static or non-static) methods will be accessible on jconsole.
                xmlns:context=”http://www.springframework.org/schema/context”
                xsi:schemaLocation=”http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd”>
               
               
               
               
                               
                               
                                               

                                                               
                                               
                               
               

harmless productivity tools ^ frameworks like ..Spring

[[The Productive Programmer]] by Neal Ford advocates learning a large number of _simple_ tools for productivity. Tools could be a complex but we use them in a non-intrusive, simple manner. Many other tools promise to help productivity but are traps – spring, hibernate, jaxb, xslt. Once you employ them they tend to become your master.

I have no such issues with perl/shell scripting, testing tools like junit, unix utilities, text editors, eclipse, maven/ant (?) A critical feature is, the option to use the tool in part of my project, and use alternative tools for the remainder. The tool should not be possessive and exclusive.

Hibernate is more gregarious than Spring, as you can mix Hibernate and straight JDBC in different modules of your system. Spring, being a framework, tends to engulf your entire project. You are then constrained by that framework developers’ decisions.

spring ApplicationContext (beyond MVC) is like a container or server

AppCx often controls some thread pool(s), esp. in trading systems. AppCx (subclass of BeanFactory) has many “early” features of a container or server —

– It is automatically started at JVM start-up
– it instantiates many objects, calling special methods on each
* It has reference to those objects and therefore serve as a crucial lookup registry or service locator
– it often remains “alive” forever as if a daemon thread

SmartLifeCycle hooks into the start-up. So does InitializationBean and init-method.

Incidentally, there are similar start-up hooks in JMS template. http://www.jarvana.com/jarvana/view/org/springframework/spring/1.2.9/spring-1.2.9-javadoc.jar!/org/springframework/jms/core/JmsTemplate.html#execute(org.springframework.jms.core.SessionCallback,%20boolean) says “…allowing the JMS Connection to be started on the fly.” — pure magic. For a topic consumer, this is the crucial but hidden start of subscription. The unsubscribe is equally hidden — when the session is closed.

how a spring app stays alive after main() method returns

In fact, the main() method doesn’t return — prevented from returning. Prevented because main() calls await() of this class below.


protected static class ContextClosedCompleteListener implements ApplicationListener {
private final CountDownLatch countDownLatch = new CountDownLatch(1);
public ContextClosedCompleteListener(ConfigurableApplicationContext applicationContext) {
applicationContext.addApplicationListener(this);
}

// required by spring's ApplicationListener
public void onApplicationEvent(ContextClosedCompleteEvent event) {
log.info("Received context closed complete event");
countDownLatch.countDown();
}

// called from main()
public void await() throws InterruptedException {
countDownLatch.await();
}
}