can we use try with resource if we pass the resource as argument ,would that close the stream before the method called completed executing - java-io

my code is below
try (ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
previoumergeDoc.setFileMereInputStream(byteStream);
service.mergeDocument(bytestream);
even though im using Try with resource, i'm getting connection reset exception

Related

How do I set up a State Store for a Transformer

I'm trying to create a Transformer, and running into problms with the initialization of its StateStore. I looked at the example in How to register a stateless processor (that seems to require a StateStore as well)?
and it makes sense, but I'm trying something different:
KeyValueBytesStoreSupplier groupToKVStore_supplier =
Stores.persistentKeyValueStore( state_store_name );
StoreBuilder< KeyValueStore< G, KeyValue< K, V > > > groupToKVStore_builder =
Stores.keyValueStoreBuilder( groupToKVStore_supplier, Gserde, KVserde );
stream_builder.addStateStore( groupToKVStore_builder );
My intention is to use a String as the State Store key and a KeyValue as the State Store value. Is the formulation above correct? I'm asking because when the stream containing my Transformer is starting up, it throws an exception that says:
Caused by: org.apache.kafka.streams.errors.TopologyBuilderException: Invalid topology building: Processor KSTREAM-TRANSFORM-0000000001 has no access to StateStore state_store_1582785598
at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.getStateStore(ProcessorContextImpl.java:72)
at com.ui.streaming.processors.sort.WindowedTimeSorter.init(WindowedTimeSorter.java:135)
at org.apache.kafka.streams.kstream.internals.KStreamTransform$KStreamTransformProcessor.init(KStreamTransform.java:51)
at org.apache.kafka.streams.processor.internals.ProcessorNode$2.run(ProcessorNode.java:54)
at org.apache.kafka.streams.processor.internals.StreamsMetricsImpl.measureLatencyNs(StreamsMetricsImpl.java:208)
at org.apache.kafka.streams.processor.internals.ProcessorNode.init(ProcessorNode.java:10
Per Matthias' suggestion, I added a StateStore name argument to the transform invocation in my Stream, and that appears to get us past the error shown above. However, we then get the following exception:
ERROR stream-thread [A.Completely.Different.appID-b04af4b4-fdbb-4353-9aa5-6d71f7c22f9e-StreamThread-1] Failed to process stream task 0_1 due to the following error: (org.apache.kafka.streams.processor.internals.AssignedStreamsTasks:105)
java.lang.IllegalStateException: This should not happen as timestamp() should only be called while a record is processed
at org.apache.kafka.streams.processor.internals.AbstractProcessorContext.timestamp(AbstractProcessorContext.java:153)
at org.apache.kafka.streams.state.internals.StoreChangeLogger.logChange(StoreChangeLogger.java:59)
at org.apache.kafka.streams.state.internals.ChangeLoggingKeyValueBytesStore.put(ChangeLoggingKeyValueBytesStore.java:69)
at org.apache.kafka.streams.state.internals.ChangeLoggingKeyValueBytesStore.put(ChangeLoggingKeyValueBytesStore.java:29)
at org.apache.kafka.streams.state.internals.InnerMeteredKeyValueStore.put(InnerMeteredKeyValueStore.java:198)
at org.apache.kafka.streams.state.internals.MeteredKeyValueBytesStore.put(MeteredKeyValueBytesStore.java:117)
at com.ui.streaming.processors.sort.WindowedTimeSorter.transform(WindowedTimeSorter.java:167)
at com.ui.streaming.processors.sort.WindowedTimeSorter.transform(WindowedTimeSorter.java:1)
at org.apache.kafka.streams.kstream.internals.KStreamTransform$KStreamTransformProcessor.process(KStreamTransform.java:56)
Alas, things are still not quite right: First off, my Transformer's init method is being called three times; it should only be once, rigt? Second, I'm getting a runtime error in my Transformer's transform method the first time it tries to store something into the StateStore:
INFO stream-thread [A.Completely.Different.appID-7dc67466-20f4-4e6c-8a69-bc0710a42f3c-StreamThread-1] Shutdown complete (org.apache.kafka.streams.processor.internals.StreamThread:1124)
Exception in thread "A.Completely.Different.appID-7dc67466-20f4-4e6c-8a69-bc0710a42f3c-StreamThread-1" java.lang.IllegalStateException: This should not happen as timestamp() should only be called while a record is processed
at org.apache.kafka.streams.processor.internals.AbstractProcessorContext.timestamp(AbstractProcessorContext.java:153)
at org.apache.kafka.streams.state.internals.StoreChangeLogger.logChange(StoreChangeLogger.java:59)
at org.apache.kafka.streams.state.internals.ChangeLoggingKeyValueBytesStore.put(ChangeLoggingKeyValueBytesStore.java:69)
at org.apache.kafka.streams.state.internals.ChangeLoggingKeyValueBytesStore.put(ChangeLoggingKeyValueBytesStore.java:29)
at org.apache.kafka.streams.state.internals.InnerMeteredKeyValueStore.put(InnerMeteredKeyValueStore.java:198)
at org.apache.kafka.streams.state.internals.MeteredKeyValueBytesStore.put(MeteredKeyValueBytesStore.java:117)
at com.ui.streaming.processors.sort.WindowedTimeSorter.transform(WindowedTimeSorter.java:155)
Just adding the store to the topology is not sufficient. You additionally need to connect the store to the transformer by passing the store name into transform():
stream.transform(..., state_store_name);
Update:
For the second exception, I assume that you don't return a new object when TransformerSupplier#get() is called, but you return the same object each time. As the "supplier pattern" suggests, you need to create a new object each time #get() is called (otherwise, a supplier would not make sense and it would be possible to hand in a single object directly). Compare the FAQ: https://docs.confluent.io/current/streams/faq.html#why-do-i-get-an-illegalstateexception-when-accessing-record-metadata

How to redirect an uploaded file in Apache MINA FTP server to a database?

I set up an FTPS server using Apache MINA. By overriding the default ftplet I can detect when a client starts uploading a new file to the server. I want to redirect the transfer to an S3 database, instead of having the file written in disk. The ftplet documentation in the MINA project states (https://mina.apache.org/ftpserver-project/ftplet.html) that
We can get the data input stream from request
But I cannot find how to get that stream from the two arguments.
Furthermore, in the FAQ there is a code example where a download is obtained from a database, by overriding the onDownloadStart method (https://mina.apache.org/ftpserver-project/faq.html#how-can-i-send-binary-data-stored-in-a-database-when-the-ftp-server-gets-the-retr-command):
public FtpletEnum onDownloadStart(FtpSession session, FtpRequest request,
FtpReplyOutput response) throws FtpException, IOException {
....
However, I am using the latest MINA version (mina-core 2.0.16, ftplet-api 1.1.1, ftpserver-core 1.1.1) and that method does not include the third argument. Has this changed in the latest versions??
The onDownloadStart example you're referring to seems to be out of date. For starters, the FtpletEnum class used was part of an early version of ftplet-api. Newer versions don't have it anymore. At least I was not able to find it.
Despite that, it's still possible to get the uploaded file from the client. You can ask for a DataConnection from the session, when overriding DefaultFtplet's onUploadStart method.
OutputStream outputStream = new ByteArrayOutputStream();
DataConnectionFactory connectionFactory = session.getDataConnection();
try {
DataConnection dataConnection = connectionFactory.openConnection();
dataConnection.transferFromClient(session, outputStream);
// now outputstream contains the uploaded file and you could
// store it in S3 if you wish
} catch (Exception e) {
e.printStackTrace();
} finally {
connectionFactory.closeDataConnection();
}
Keep in mind that you might also have to notify the client with response codes if your onUploadStart method returns SKIP. From Ftplet docs
This method will be called before the file upload. The file name can be get from the request argument. We can get the data input stream from request. This will be called before the permission check. This is called during STOR command. If the method returns SKIP, it has to send responses before and after processing. For example, before opening the data input stream, the method has to notify the client with a response code 150. Similarly, after the data transfer, the method has to notify the client with a response code 226. In case of any error, the method should send different response codes like 450, 425, 426, 551.

Application.Current.Properties - System.AggregateException

I'm trying to get some data from Application.Current.Properties storage. Unfortunately, any time I want to use this Dictionary, I see this error:
An exception of type 'System.AggregateException' occurred in mscorlib.ni.dll but was not handled in user code
Additional information: One or more errors occurred.
And in details I found this:
{"Error in line 1 position 206. Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value' contains data of the 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfstring' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'ArrayOfstring' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer."}
It seems like I tried to save some non-string data to Application.Current.Properties. Unfortunately I can't run .Clear() method to erease all data, bacause I receive this error any time I'm trying to access this property.
What should I do to make it work?
Well, as its name suggests AggregateException, is just a container for one or more exceptions which may be thrown when using PLINQ or TPL.
As such exceptions may be thrown on different threads and may also occur concurrently, the system automatically catches and rethrows them within an AggregateException wrapper to ensure that they all get reported in one place. The exceptions themselves are exposed via the InnerExceptions property.
You can catch an AggregateException and check which exceptions it actually contains with code such as the following:
try
{
// perform some parallel operation
}
catch (AggregateException aex)
{
string messages = "";
foreach(Exception ex in aex.InnerExceptions)
{
messages += ex.Message + "\r\n";
}
MessageBox.Show(messages);
}
So I suggest you do this to see what is causing the problem
Please, remove your app from your device, Settings - Applications- Uninstall, this works for me. The Auth Object was crash in debug mode.Clean and Rebuild can be Helpfull to.

ARM in JAVA 7(try with resources),error while re-assigning value to any variable of AutoCloseable type

I was working on the new try with resources block which automatically invokes close() method of AutoCloseable
I wanted to know that why doesnt the compiler allow me to re-assign a value to the varable after has been declared.
Check this out:-
try (FileInputStream fs = new FileInputStream("c:\\code\\audit.txt");ObjectInputStream is= new ObjectInputStream(fs);fs = new FileInputStream("c:\\code\\log.txt")) {
Compilation error:-
VariableDeclaratorId expected after this token
fs cannot be resolved to a type
As mentioned in API
The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement.
It clearly says, it only declares variables, not assigned.
So this gives compilation error. Here is use-full link which explains that.
Additionally, Then it says,
Note that the close methods of resources are called in the opposite order of their creation.
So its obvious, so it can't close a resource which already closed.
if Resource reference variable start reassigning then resource which was previously pointing resource will be opened still and that will be waist , so only new pointing resource will be closed , due to this reason compiler raise Compile time error. "auto-closeable resource xxx may not be assigned" . based on java 1.7

Exception DateTime OleDbParameter

I’m using Visual Studio 2010. Within the project we add a DataSet, inside it; we have a Query Table Adapter to do all the queries to a SQL Server 2000 Data Base. One of the queries is formed using a Stored Procedure that receives four parameters. One of the parameters is a DateTime data type. Although we have check many times, we are receiving an unexpected exception:
Provider encountered an error while sending command parameter[0] '' value and stopped processing.
Conversion failed for command parameter[1] '' because the data value overflowed the type used by the provider.
Provider encountered an error while sending command parameter[2] '' value and stopped processing.
Provider encountered an error while sending command parameter[3] '' value and stopped processing.
Provider encountered an error while sending command parameter[4] '' value and stopped processing.
Working around this, if we delete the DateTime parameter of the Store Procedure, the query executes successfully otherwise we get the exception mentioned before.
We notice that the DateTime parameter has the property set as follows:
DbType: DateTime
ProviderType: DBTimeStamp
Any approach trying to accomplish the execution of the Query will be greatly welcome.
When I received this error, I had to change my date parameter to tell what the data type was.
Old Way
cmd.Parameters.Add(new OleDbParameter("TDate", DateTime.Now));
New Way
OleDbParameter dateParam = new OleDbParameter("TDate", OleDbType.Date);
dateParam.Value = DateTime.Now;
cmd.Parameters.Add(dateParam);

Resources