我需要使用java nio将大字符串写入(追加)平面文件.编码为ISO-8859-1.
目前我们写的如下所示.有没有更好的方法来做同样的事情?
public void writeToFile(Long limit) throws IOException{
String fileName = "/xyz/test.txt";
File file = new File(fileName);
FileOutputStream fileOutputStream = new FileOutputStream(file, true);
FileChannel fileChannel = fileOutputStream.getChannel();
ByteBuffer byteBuffer = null;
String messageToWrite = null;
for(int i=1; i<limit; i++){
//messageToWrite = get String Data From database
byteBuffer = ByteBuffer.wrap(messageToWrite.getBytes(Charset.forName("ISO-8859-1")));
fileChannel.write(byteBuffer);
}
fileChannel.close();
}
Run Code Online (Sandbox Code Playgroud)
编辑:尝试了两种选择.以下是结果.
@Test
public void testWritingStringToFile() {
DiagnosticLogControlManagerImpl diagnosticLogControlManagerImpl = new DiagnosticLogControlManagerImpl();
try {
File file = diagnosticLogControlManagerImpl.createFile();
long startTime = System.currentTimeMillis();
writeToFileNIOWay(file);
//writeToFileIOWay(file);
long …Run Code Online (Sandbox Code Playgroud) 假设我有一个字符串,它是等供给"$123,456,56.25"或"123'456.67"或与此类似(用数字和小数点,一些分隔符像,或者'还是其他什么东西是不可预测的).我需要编写一个方法,它接受类似上面的参数并返回一个类似于"12345656.25"或的字符串"123456.67".
能否请您建议最有效和最易读的代码来实现这一目标?
注意:我知道要检查每个索引并检查它是否为其重新调整true,Character.isDigit(charAtInedx)或者if(charAtInedx == '.')
我在寻找效率和可读性方面的更优化解决方案
谢谢.
我有一个应用程序"persisting to database"占用了整个应用程序流程的85%时间.
我正在考虑使用多个线程来执行插入,因为插入在这里大多是独立的.有没有办法使用任何JPA实现实现多线程插入?或者从提高性能角度来看,是否值得做mutli线程插入?
注意:插入在一次运行中的记录范围为10K到100K.此外,性能非常关键.
谢谢.
以下是我的Spring上下文.xml文件的摘录.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<beans:import
resource="classpath:com/batch/jobs/data-source-context.xml" />
<job id="xxxx">
<step id="loadRecord">
<tasklet>
<chunk reader="dtaFileItemReader" writer="dtaGroupWriter"
commit-interval="${job.commit.interval}" />
</tasklet>
</step>
</job>
<jee:jndi-lookup id="dataSource" jndi-name="jdbc"></jee:jndi-lookup>
<beans:bean id="incrementerParent" class="${batch.database.incrementer.class}">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="incrementerName" value="ID" />
</beans:bean>
Run Code Online (Sandbox Code Playgroud)
我得到一个例外说:
nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'jee:jndi-lookup'
Run Code Online (Sandbox Code Playgroud) 我们在管理测试数据时遇到了一个问题(xmls用于创建模拟对象).我们目前的数据已经在很长一段时间内得到了发展.每次我们添加新功能或测试用例时,我们都会添加新数据来测试该功能.现在,问题是当业务需求改变格式(如变量的长度或格式)或测试数据不支持的任何更改时,我们需要更改整个测试数据的大小为100的MB.有谁能建议一个更好的方法或过程来克服这个问题?任何建议将不胜感激.
我正在尝试在JBOSS 7中创建一个数据源.
我的standalone.xml摘录:
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="MySqlDS" pool-name="MySqlDS" enabled="true" jta="true" use-java-context="true" use-ccm="true">
<connection-url>jdbc:mysql://localhost:3306/sampledb</connection-url>
<driver>mysql</driver>
<transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
<pool>
<prefill>true</prefill>
<use-strict-min>false</use-strict-min>
<flush-strategy>FailingConnectionOnly</flush-strategy>
</pool>
<security>
<user-name>root</user-name>
<password>matrix</password>
</security>
</datasource>
<drivers>
<driver name="mysql" module="com.mysql">
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
Run Code Online (Sandbox Code Playgroud)
我已经创建了module/com/mysql/main目录,并将mysql jar和module.xml放在一起,如下所示:
<module xmlns="urn:jboss:module:1.0" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-3.0.17-ga-bin.jar"/>
<!-- Insert resources here -->
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
Run Code Online (Sandbox Code Playgroud)
我的persistence.xml使用这个数据源
<jta-data-source>java:/MySqlDS</jta-data-source>
Run Code Online (Sandbox Code Playgroud)
但是服务器启动会抛出错误消息,如下所示.
New missing/unsatisfied dependencies:
service jboss.jdbc-driver.mysql (missing)
Run Code Online (Sandbox Code Playgroud)
能否请你帮忙 ?
谢谢.
我有批处理作业,它从批量文件中读取数据,处理它并插入数据库.
我使用默认分区处理程序使用spring的分区功能.
<bean class="org.spr...TaskExecutorPartitionHandler">
<property name="taskExecutor" ref="taskExecutor"/>
<property name="step" ref="readFromFile" />
<property name="gridSize" value="10" />
</bean>
Run Code Online (Sandbox Code Playgroud)
gridSize这里有什么意义?我已经以这样的方式配置它等于taskExecutor中的并发性.
我的用例要求我有一个类层次结构,如下所示
public interface ServiceA{ public void doSomething();}
public abstract class AbstractClass implements ServiceA{
@Override
public void doSomething(){
getMetaValue();
.. do common Things applicable to all EJBS...
}
public abstract MeataValue getMetaValue();
}
@Stateless(mappedName="EJBBeanImlJ")
public EJBBeanImplJ extends AbstractClass{
public MetaValue getMetaValue(){
return new MetaValue(x, y);
}
}
@Stateless(mappedName="EJBBeanImplK")
public EJBBeanImplK extends AbstractClass{
public MetaValue getMetaValue(){
return new MetaValue(a,b);
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
为两个EJB实现提供相同的接口是一个很好的EJB实践吗?
您是否在课程设计/层次结构中看到任何其他缺点?
注意:我的appserver是Weblogic
谢谢
使用spliterator()通过Iterable 创建的流时,我遇到了性能问题 .就像StreamSupport.stream(integerList.spliterator(), true).想要在正常的系列中证明这一点.请参阅下面的一些基准测试结果
问题:为什么从迭代创建的并行流比从ArrayList或IntStream创建的流慢得多?
从一个范围
public void testParallelFromIntRange() {
long start = System.nanoTime();
IntStream stream = IntStream.rangeClosed(1, Integer.MAX_VALUE).parallel();
System.out.println("Is Parallel: "+stream.isParallel());
stream.forEach(ParallelStreamSupportTest::calculate);
long end = System.nanoTime();
System.out.println("ParallelStream from range Takes : " + TimeUnit.MILLISECONDS.convert((end - start),
TimeUnit.NANOSECONDS) + " milli seconds");
}
Run Code Online (Sandbox Code Playgroud)
Is Parallel:true
范围内的ParallelStream Takes:490毫秒
来自Iterable
public void testParallelFromIterable() {
Set<Integer> integerList = ContiguousSet.create(Range.closed(1, Integer.MAX_VALUE), DiscreteDomain.integers());
long start = System.nanoTime();
Stream<Integer> stream = StreamSupport.stream(integerList.spliterator(), true);
System.out.println("Is Parallel: " + stream.isParallel());
stream.forEach(ParallelStreamSupportTest::calculate);
long end = …Run Code Online (Sandbox Code Playgroud) 我有一个问题,我需要从MQ队列接收一系列消息并将其写入文件并启动弹出批处理作业,并将该文件作为输入.现在我正在考虑使用@Autowired JobLauncher jobLauncher and @Autowired Job job;MDB本身的有线连接来启动这项工作.但我觉得这不是一个好方法,因为Spring批处理可能会创建一系列线程,因此EJB不支持多线程.
有没有其他有效的方法来做到这一点?我不想使用石英调度程序或其他任何东西,因为它增加了复杂性.春天批处理中是否有任何接口在文件进入目录后很快就会启动作业?任何做得更好的线索将不胜感激.
谢谢.
首先是一点背景.
我们正在开发一个接收来自n多个消息的应用程序sources.该source会是一个messaging queue,一个FTP位置,webservice特定服务或者我们能想到的任何可能的编排层调用.我被赋予了设计和开发模块的任务,该模块将充当可配置的资源管理器,该模块将在处理消息的模块和发送消息的应用程序之间工作.
您能否提出我可以在这里使用的任何设计模式或任何最佳实践.我们希望能够灵活地配置此资源并动态更改频道.表示今天消息类型A是否进入队列,明天这可能是预定的Web服务调用.
在这方面的任何指针将不胜感激.
我的应用程序在Oracle上有几个表,其中用户XYZ是架构所有者.使用XYZ创建表.我想让ABCUSER在这些表上拥有CRUD权限.我已经通过访问权限GRANT ALL ON TABLEABC to ABCUSER并且授予成功.
但是当这个用户(ABCUSER)尝试查询数据库(从TABLEABC中选择*)时,它似乎不起作用.我收到错误消息
ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action:
Error at Line: 1 Column: 14
Run Code Online (Sandbox Code Playgroud)
你能告诉我我错过了什么吗?
我正在维护一个几乎没有作为SOAP Web服务公开的服务的应用程序.我最近遇到了一些扩展问题,因为应用程序已经接收到比正常情况更多的负载.
关于少数池配置,我想知道我的理解是否正确,1.我们有如下的线程池配置,
<thread-pools>
<thread-pool name="admin-thread-pool" max-thread-pool-size="50" max-queue-size="1024"></thread-pool>
<thread-pool name="http-thread-pool" max-thread-pool-size="250"></thread-pool>
<thread-pool name="http-thread-pool-internal" max-thread-pool-size="50"></thread-pool>
<thread-pool name="thread-pool-1" max-thread-pool-size="200"></thread-pool>
</thread-pools>
Run Code Online (Sandbox Code Playgroud)
和
<transports>
<transport name="tcp" acceptor-threads="8"></transport>
</transports>
Run Code Online (Sandbox Code Playgroud)
2. EJB池配置如下,
<ejb-container steady-pool-size="0" max-pool-size="50" pool-resize-quantity="10">
Run Code Online (Sandbox Code Playgroud)
所以现在问题.
如果http线程池收到需要同步执行的任务,并且池中没有足够的EJB bean实例(最大配置为50),会发生什么情况,因为所有EJB实例都在为其他http请求提供服务?注意:我们正在进行JNDI查找而不使用@EJB注释.
增加EJB实例(max)的数量是否等于http-threadpool值是否有意义?
在进行一些分析后,发现查找EJB实例的代码需要很长时间才能返回.这是否意味着没有可用的EJB实例,并且请求必须等到其他正在运行的线程释放实例?
java ×12
spring ×3
ejb ×2
oop ×2
spring-batch ×2
channel ×1
ejb-3.0 ×1
file-io ×1
glassfish ×1
input ×1
java-8 ×1
java-stream ×1
jboss ×1
jpa ×1
junit ×1
mysql ×1
nio ×1
ora-00942 ×1
oracle ×1
performance ×1
sql ×1
string ×1
testing ×1
threadpool ×1
unit-testing ×1